pku1009

此题一个个算像素的值会超时,所以要寻找规律。

注意到有很多都是连续相同的值,所以要考虑什么时候值会发生变化,然后得出结果。从位置1到与它相邻的位置2值发生变化,肯定有一个输入值变化了。如果用编程来解的话,就是输入值变化时,计算其影响到的相邻八块的值及其本身的值,思路和另外一篇博客类似。http://blog.csdn.net/xudacheng06/article/details/6711419

但它的实现明显更简单,比如map的灵活运用,通过计算总数来得到每个位置所在的序号。。。。等等.刚开始提交,结果正确,但总是wrong answer,很纳闷。后来才发现时边界条件有错误,要注意程序的严谨性啊。代码如下:

#include<iostream>
#include<map>
#include<cmath>
using namespace std;
#define N 1002
int Pair[N][2];
int col;
int sum;
int count;
bool isinpic(int x,int y){
	if( x>=1 && y>=1 && x<=col && ((y-1)*col+x)<=sum )
		return true;
	return false;
}
int getvalue(int x,int y){
	int n = (y-1)*col+x;
	for(int i = 1;i<sum;i++){
		if(n <= Pair[i][1])
			return Pair[i][0];
	}
	return -1;
}
int getmaxdis(int x,int y){
	int value = getvalue(x,y);
	int max  = -1;
	for(int dx = x-1;dx <= x+1;dx++)
		for(int dy = y-1;dy <= y+1;dy++){
			if(isinpic(dx,dy)){
				int dis = abs(getvalue(dx,dy)-value);
				if(dis > max){
					max = dis;
				}
			}
		}
	return max;
}
int main()
{
	int value,num;
	map<int,int> test;
	map<int,int>::iterator vis1;
	map<int,int>::iterator vis2;
	while(cin>>col){
		if(col == 0)
			break;
		Pair[0][1] = 0;
		sum = 0;
		count = 1;
		test.clear();
		while(cin>>value>>num){
			if(value == 0 && num == 0)
				break;
			Pair[count][0] = value;
			sum += num;
			Pair[count][1] = sum;
			count++;
		}
		cout<<col<<endl;
		{
			for(int i = 0;i <count;i++){
				int n = Pair[i][1]+1;
				int dx,dy;
				if(n%col ==0){
					dx = col;
					dy = n/col;
				}
				else{
					dx = n%col;
					dy = n/col+1;
				}
				for(int x = dx-1;x<= dx+1;x++)
					for(int y = dy-1;y<=dy+1;y++){
						if(isinpic(x,y)){
							test.insert(make_pair((y-1)*col+x,getmaxdis(x,y)));
						}
					}
			}
			vis1 = test.begin();
			while(vis1 != test.end()){
				vis2 = vis1;
				vis2++;
				for(;vis2 != test.end()&& vis1->second == vis2->second;vis2++);
				if(vis2 == test.end()) break;
				cout<<vis1->second<<" "<<(vis2->first-vis1->first)<<endl;
				vis1 = vis2;
			}
			cout<<vis1->second<<" "<<(sum-vis1->first+1)<<endl;
		}
		cout<<"0 0"<<endl;
	}
	cout<<"0"<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值