[ACM]The Best Seat in ACM Contest

2024-01-28 14:08
文章标签 acm contest best seat

本文主要是介绍[ACM]The Best Seat in ACM Contest,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

Cainiao is a university student who loves ACM contest very much. It is a festival for him once when he attends ACM Asia Regional Contest because he always can find some famous ACMers there.
Cainiao attended Asia Regional Contest Fuzhou Site on November 20, 2011. After he got seat map, he wanted to know which seat is the best one.
Cainiao have joined so many QQ Group about ACM/ICPC that he is almost familiar with the strength of each team. In his mind, the value of a seat is defined as following:

1. Strength of each team can be expressed as a positive integer.
2. The value of a seat is related to the adjacent seat (up/down/left/right, only four directions being considering).
3. For an adjacent seat, if the strength of this team is stronger than yours, the absolute value of difference of two teams should be added to your seat, otherwise, the absolute value of difference should be subtracted from your seat.
4. If the adjacent seat is empty (which means you are located at the most left/right/up/down), the value of your seat should be subtracted 1.
5. The best one in a contest is the seat that has the highest value.
6. The initial value of the seat is ZERO.

For example, there are 12 ( 3 X 4 ) teams in a contest, the strength of each team is as figure (a), and then you can calculate the value of each seat as figure (b).

 

输入

Input contain a positive integer T( T <=50 ) in the first line, which means T cases.
The first line of each case contains two positive integers N and M (3 <= N, M <= 20) which means the row and column number of the teams, then N rows following, each line contains M positive integers that represent the strengths of the teams.

输出

For each case, first output the case number, and then output the value and row number and column number of the best seat in one line for each case. 
If there are multiple solutions for one case, you should output the seat whose row number is largest and only output the seat whose column number is largest if still overlapping.

示例输入

13 41 5 3 46 3 3 44 3 2 1

示例输出

Case 1: 7 1 1

 

解题思路:

使用两数组a,b,a用来保存原数据,b用来存放相应位置每个原数据计算后的数据,即保存每个原数据的状态变量。对数据进行分情况。1,四个角的数据,需要进行2次运算。2,最外面四条边上,去掉角剩下的中间数据,需要进行三次运算。3,去掉外面的一圈数据剩下的原数据,需要进行四次运算,上下左右。一开始在算法上出了错误,不是不对,而是特别麻烦,折腾的头晕眼花,每次计算都用if 怎么样,然后else 怎么样,后来慢慢发现,其实不用判断所要进行计算的原数据的相邻数据是否比它大还是小,直接用相邻数据减去原数据即可,大大减少了代码量,而且效率高。

代码:

#include <iostream>
using namespace std;
int a[21][21];//用来放原数据
int b[21][21];//用来放计算后的数据
int main()
{
int T,t;
cin>>T;
for(t=1;t<=T;t++)
{
int i,j,i1,j1;//数组i行,j列
cin>>i>>j;
for(i1=0;i1<i;i1++)
for(j1=0;j1<j;j1++)
{
cin>>a[i1][j1];
b[i1][j1]=0;//数组b初值全部为0
}
b[0][0]+=-2;//考虑数组的左上角数据,因为有两侧没有相邻数据,所以减去2分
b[0][j-1]+=-2;//右上角
b[i-1][0]+=-2;//左下角
b[i-1][j-1]+=-2;//右下角
b[0][0]+=a[0][1]-a[0][0];//下面分别对四个角的数据计算,每个数据计算两次
b[0][0]+=a[1][0]-a[0][0];//计算的时候不需比较相邻数据,直接用相邻数据减去本数据即可满足题目要求,b数组保留每次计算的结果
b[i-1][0]+=a[i-2][0]-a[i-1][0];
b[i-1][0]+=a[i-1][1]-a[i-1][0];
b[0][j-1]+=a[0][j-2]-a[0][j-1];
b[0][j-1]+=a[1][j-1]-a[0][j-1];
b[i-1][j-1]+=a[i-2][j-1]-a[i-1][j-1];
b[i-1][j-1]+=a[i-1][j-2]-a[i-1][j-1];
for(i1=1;i1<=i-2;i1++)//考虑数组最左边中间的数据,每个数据需要计算三次
{
b[i1][0]+=-1;//一侧没有相邻数据,减去1分
b[i1][0]+=a[i1][1]-a[i1][0];//右减左
b[i1][0]+=a[i1-1][0]-a[i1][0];//上减下
b[i1][0]+=a[i1+1][0]-a[i1][0];//下减上
}
for(j1=1;j1<=j-2;j1++)//最上边中间的数据
{
b[0][j1]+=-1;
b[0][j1]+=(a[1][j1]-a[0][j1]);
b[0][j1]+=(a[0][j1-1]-a[0][j1]);
b[0][j1]+=(a[0][j1+1]-a[0][j1]);
}
for(j1=1;j1<=j-2;j1++)//最下边中间的数据
{
b[i-1][j1]+=-1;
b[i-1][j1]+=a[i-2][j1]-a[i-1][j1];
b[i-1][j1]+=a[i-1][j1-1]-a[i-1][j1];
b[i-1][j1]+=a[i-1][j1+1]-a[i-1][j1];
}
for(i1=1;i1<=i-2;i1++)//最右边中间的数据
{
b[i1][j-1]+=-1;
b[i1][j-1]+=a[i1][j-2]-a[i1][j-1];
b[i1][j-1]+=a[i1-1][j-1]-a[i1][j-1];
b[i1][j-1]+=a[i1+1][j-1]-a[i1][j-1];
}//到这里已经把a数组里的最外面一圈的数据处理完毕,把处理结果都放到相应位置的b数组中了
for(i1=1;i1<=i-2;i1++)//处理内部数据
for(j1=1;j1<=j-2;j1++)
{
b[i1][j1]+=a[i1-1][j1]-a[i1][j1];//每个数组都需要计算四次
b[i1][j1]+=a[i1+1][j1]-a[i1][j1];
b[i1][j1]+=a[i1][j1-1]-a[i1][j1];
b[i1][j1]+=a[i1][j1+1]-a[i1][j1];
}//到这里a数组里的全部数据计算完毕
int m,n,max,x,y;//x,y用来标记b数组中最大数的 行数 和 列数,max用来求b数组中的最大值
max=b[0][0];
for(m=0;m<=i-1;m++)
for(n=0;n<=j-1;n++)//遍历b数组
{
if(max<=b[m][n])
{
max=b[m][n];
x=m;
y=n;
}
}
cout<<"Case "<<t<<": "<<max<<" "<<x+1<<" "<<y+1<<endl;
}
return 0;
}


 

运行截图:

这篇关于[ACM]The Best Seat in ACM Contest的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/653853

相关文章

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

2014 Multi-University Training Contest 8小记

1002 计算几何 最大的速度才可能拥有无限的面积。 最大的速度的点 求凸包, 凸包上的点( 注意不是端点 ) 才拥有无限的面积 注意 :  凸包上如果有重点则不满足。 另外最大的速度为0也不行的。 int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;}struct poin

2014 Multi-University Training Contest 7小记

1003   数学 , 先暴力再解方程。 在b进制下是个2 , 3 位数的 大概是10000进制以上 。这部分解方程 2-10000 直接暴力 typedef long long LL ;LL n ;int ok(int b){LL m = n ;int c ;while(m){c = m % b ;if(c == 3 || c == 4 || c == 5 ||

2014 Multi-University Training Contest 6小记

1003  贪心 对于111...10....000 这样的序列,  a 为1的个数,b为0的个数,易得当 x= a / (a + b) 时 f最小。 讲串分成若干段  1..10..0   ,  1..10..0 ,  要满足x非递减 。  对于 xi > xi+1  这样的合并 即可。 const int maxn = 100008 ;struct Node{int

AtCoder Beginner Contest 370 Solution

A void solve() {int a, b;qr(a, b);if(a + b != 1) cout << "Invalid\n";else Yes(a);} B 模拟 void solve() {qr(n);int x = 1;FOR(i, n) FOR(j, i) qr(a[i][j]);FOR(i, n) x = x >= i ? a[x][i]: a[i][x];pr2(

【转载】ACM感悟

今天看了一篇我们学校前辈的ACM的感悟,觉得写的十分有道理,这里转载,文章还会不断的改进和更新。 原文链接:http://www.cnblogs.com/Chierush/p/3760870.html?ADUIN=1339764596&ADSESSION=1401536826&ADTAG=CLIENT.QQ.5329_.0&ADPUBNO=26349 声明:本文是写给弱校ACM新手的一点

我们依旧在追梦的路上-山东省第六届ACM比赛总结

这场比赛从结果而言达到了预期(金牌),从过程而言和我的预期相差甚远(打的太乱,个人发挥很差),还好关键时刻队友抗住压力,负责后果真的不堪设想。 热身赛 热身赛纯粹测机器的,先把A,B,C草草水过(A题小写x打成大写的也是醉了),我和老高开始各种测机器,long long不出所料是lld的,试了一下除0和数组越界的re问题,发现没有re,只有wa(甚至数组越界还AC了),至于栈深的话也没过多追

CF Bayan 2015 Contest Warm Up B.(dfs+暴力)

B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/475/probl

CF Bayan 2015 Contest Warm Up A.(模拟+预处理)

A. Bayan Bus time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/475/problem/A The fi

ACM东北地区程序设计大赛

不得不说随着参赛级别的提高,题目真的是越来越难啊,不过队长真是给力啊,在我们三个共同努力之下拿下了地区赛三等奖,哈哈我们可是大一唯一一只获奖队,终于在这次比赛打败了田大神。。。大神是失手了,俺和他差距还是挺大的。。。队友陈彤马上要去服兵役了,他说这是我们送给他最好的离别礼物,希望那家伙在部队好好干,以后谁干揍我!!!东北地区赛结束后,今年已经估计没机会参加亚洲区比赛了,赶紧补高数和线数啊!!别挂了