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 ...
随机推荐
- tableView嵌套collectionView
首先是自定义collectionView填充的tableViewCell import UIKit // 定义一个collectionView,重写初始化大小和布局方法 class TrendsDet ...
- 一些javascript免费中文书籍
在这里谢谢那些无私的人~~这些内容来自这里:我只把js的链接粘到这里了~ 还有其它技术文档, 实在是太多了, 多的看都看不完!!! Google JavaScript 代码风格指南 Google JS ...
- mysql锁死的现象判断
一般发生表锁死这种低级问题,就有两种情况:1.程序员水平太菜,2.程序逻辑错误. 一旦发生系统会出现超时,关键是有可能你看不到正在活动的php进程,而系统的慢查询日志也不会记录,只能通过show fu ...
- 全部与精简切换显示jQuery实例教程
下面是某网站上的一个品牌列表展示效果,用户进入页面时,品牌列表默认是精简显示的(即不完整的品牌列表)效果如下图所示: 用户可以单击商品列表下方的“显示全部品牌”按钮来显示全部的品牌.单击“显示全部品牌 ...
- SQL 结构化查询语言
SQL 结构化查询语言 一.数据库的必要性: >>作用:存储数据.检索数据.生成新的数据 1)可以有效结构化存储大量的数据信息,方便用户进行有效的检索和访问. 2)可以有效地保持数据信息的 ...
- 【随记】解决:VS2010 调试器无法继续继续运行该进程,无法启动调试
今天在调试项目的时候突然出现错误: 按照网上的一些方法弄了后还是同样报错,把本地代码删除后从库上重现拉下来的项目依然报错,到这里就明白不是项目本身问题了,而是VS2010 的问题,经过网上查资料,问同 ...
- 小心DriveInfo类IsReady属性的较大延迟问题
当某些驱动器调用IsReady属性来判断是否准备好时,会有性能问题,会非常慢,特别是网络驱动器断开的时候,这个属性会有30秒左右的延迟,这对程序执行是非常大的开销,请慎重调用
- FC8下备份linux系统
linux系统可以使用tar来备份.<br><br> 我在FC8上装好了totem, mplayer, audacious, 并搞定了wifi后,我觉得该备份一下FC8系统.& ...
- <select>与<datalist>的区别
size:下拉框中每次出现选项的个数 multiple:可以一次性选多个选项: disabled:时下拉框不可用,无法点击选项 list:它的值应于id的值对应 datalist要与input标签一 ...
- PHPMailer发匿名邮件及Extension missing: openssl的解决
原文链接:http://www.tongfu.info/phpmailer%E5%8F%91%E5%8C%BF%E5%90%8D%E9%82 %AE%E4%BB%B6%E5%8F%8Aextensio ...