poj1009

#include<iostream>
#include<queue>
#include<cmath>
#include<algorithm>
#include<string.h>
using namespace std; #define maxn 1005
#define INF 0xfffffff int dir[9][2] = {
{-1,-1},{-1, 0},{-1, 1},
{ 0,-1},{ 0, 0},{ 0, 1},
{ 1,-1},{ 1, 0},{ 1, 1}
}; struct Set
{
int L, R;
int vl, cnt;
}a[maxn]; struct node
{
int vl, cnt;
}; struct SQLine
{
int Up, Down;
}; int FindSet(int L, int R, int e); //查找e在第几个区间
int MaxAbs(int k, int wide, int Len, int n); //与四周相比最大的绝对值
int FindLocation(int L, int R, int wide, int l, int r); //查找与e相等的最右边的位置
node Fill(int k, int wide, int cnt, int Len, int n); //填充
int OK(int x, int y, int wide, int Len); //判断xy坐标是否合法 int main()
{
int wide; while(cin >> wide)
{
int n=1;
node s;
queue<node> Q; if(wide == 0)
{
cout << "0" <<endl;
continue;
}
a[0].L = -INF, a[0].R = -1;
while(cin >> a[n].vl >> a[n].cnt, a[n].vl+a[n].cnt)
{
a[n].L = a[n-1].R+1;
a[n].R = a[n-1].R+a[n].cnt;
n++;
}
a[n].L = a[n-1].R+1, a[n].R=INF;
int k=0, End = a[n-1].R;
while(k <= End)
{
int indexK = FindSet(0, n, k); int Len = FindLocation(k, a[indexK].R, wide, 0, n); Q.push(Fill(k++, wide, 1, End/wide+1, n)); if(Len - k == 0)
Q.push(Fill(k++, wide, 1, End/wide+1, n));
else if(Len - k > 0)
{
Q.push(Fill(k, wide, Len-k, End/wide+1, n));
Q.push(Fill(Len, wide, 1, End/wide+1, n));
k = Len+1;
}
}
s.cnt = s.vl = 0;
Q.push(s);
s = Q.front();
s.cnt = 0; cout << wide <<endl; while(Q.size())
{
node q = Q.front();Q.pop();
if(s.vl == q.vl)
s.cnt += q.cnt; if(s.vl != q.vl || Q.size() == 0)
{
cout<< s.vl << ' ' << s.cnt <<endl;
s = q;
} if(Q.size() == 0)
cout << "0 0" <<endl;
}
} return 0;
} int FindSet(int L, int R, int e) //查找e在第几个区间
{
while(L<=R)
{
int Mid = (L+R) / 2; if(e >= a[Mid].L && e <= a[Mid].R)
return Mid;
else if(e < a[Mid].L)
R = Mid-1;
else
L = Mid+1;
} return 0;
}
int MaxAbs(int k, int wide, int Len, int n) //与四周相比最大的绝对值
{
int i, x = k/wide, y=k%wide;
int MaxNum=0;
int t = FindSet(0, n, k); for(i=0; i<9; i++)
{
int nx = x+dir[i][0];
int ny = y+dir[i][1]; if(OK(nx, ny, wide, Len))
{
int j = nx * wide + ny;
j = FindSet(0, n, j);
MaxNum = max(MaxNum, abs(a[t].vl - a[j].vl));
}
} return MaxNum;
}
int OK(int x, int y, int wide, int Len) //判断xy坐标是否合法
{
if(x>=0&&x<Len && y>=0&&y<wide)
return 1;
return 0;
}
int FindLocation(int L, int R, int wide, int l, int r) //查找与e相等的最右边的位置
{
int Mid, up=FindSet(l, r, L-wide), down=FindSet(l, r, L+wide);
int ans = L; while(L <= R)
{
Mid = (L+R) / 2; int i=FindSet(l, r, Mid-wide), j=FindSet(l, r, Mid+wide); if(up == i && down == j)
L = Mid+1, ans=Mid;
else
R = Mid-1;
} return ans;
}
node Fill(int k, int wide, int cnt, int Len, int n) //填充
{
node s; s.vl = MaxAbs(k, wide, Len, n);
s.cnt = cnt; return s;
}
poj1009的更多相关文章
- 【poj1009】 Edge Detection
http://poj.org/problem?id=1009 (题目链接) 不得不说,poj上的水题还是质量非常高的= =,竟然让本大爷写了一下午. 转自:http://blog.sina.com.c ...
- POJ1009 Edge Detection
题目来源:http://poj.org/problem?id=1009 题目大意: 某图像公司用run length encoding(RLE)的方式记录和存储了大量的图像.程序的目标是读入压缩后的图 ...
- 北大poj- 1009
Edge Detection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22835 Accepted: 5398 D ...
- poj 算法 分类
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 最近AC题:2528 更新时间:2011.09.22 ...
- POJ 水题(刷题)进阶
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
- 北大ACM试题分类+部分解题报告链接
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
- 北大ACM - POJ试题分类(转自EXP)
北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...
- 北大ACM - POJ试题分类
1.入门水题 可用于练手与增强自信 POJ-1003POJ-1004 POJ-1005 POJ-1207 POJ-3299 POJ-2159 POJ-1083POJ-3094 2.初级 2.1. 基本 ...
- poj1000-1009小结
poj1000-1009小结 poj1000-1009小结 poj1000 AB poj1001 Exponentiation poj1002 poj1003 poj1004 Financial Ma ...
随机推荐
- jQuery慢慢啃之文档处理(五)
1.append(content|fn)//向每个匹配的元素内部追加内容. $("p").append("<b>Hello</b>"); ...
- PHP+MySQL中对UTF-8,UTF8(utf8),set names gbk 的理解
问题一:在我们进行数据库操作时会发现,数据库中表的编码用的是utf-8,但是在进行dos命令是要使用set names gbk (一)Mysql中默认字符集设置有四级:服务器级,数据库级,表级,和字段 ...
- 使用BeanUtils组件
使用BeanUtils组件 前提 1:导入commons-beanutils-1.8.3.jar //根据 本人使用的是1.8.3的版本 2:导入日志包 //就是loggin ...
- 调用数据库过程函数mysql
Connection conn=JdbcUtil.getConnection();//JdbcUtil是我写的获取connection的工具类 CallableStatement cast=conn. ...
- 《zip命令》-linux命令五分钟系列之九
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...
- .net软件工程师面试题(参考答案)
一.填空题(每空1分,共12分) 1面向对象的语言具有__封装______性.__继承_______性.__多态______性. 2能用foreach遍历访问的对象需要实现 ____Ienumerab ...
- PHP第一课笔记
打算以后学习PHP,花3个月时间学会它,自己为自己加油.每天坚持学习,第一天感觉良好,没开始写,所以不敢觉难,在难也学,加油,me!! PHP笔记记录(2014.7.27) ★web开发的介绍 1.动 ...
- 2基本概念--python深度机器学习
参考彭亮老师的视频教程:转载请注明出处及彭亮老师原创 视频教程: http://pan.baidu.com/s/1kVNe5EJ 基本概念:训练集,测试集,特征值,监督学习,非监督学习,半监督学习,分 ...
- Python Tutorial 学习(九)--Classes
## 9. Classes 类 Compared with other programming languages, Python's class mechanism adds classes wit ...
- Http和Socket连接的区别
Http和Socket连接区别 相信不少初学手机联网开发的朋友都想知道Http与Socket连接究竟有什么区别,希望通过自己的浅显理解能对初学者有所帮助. 1.TCP连接 要想明白Socket连接,先 ...