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 ...
随机推荐
- 某PHP代码加密
<?php /* 本程序已加密: 2014-11-15 10:10:11 */ xs_run('JGxosS9QplmqLA6qjYo/LiX5ecUe0DH7p42Ww/Mdkf5/ybZDs ...
- iOS6以后的单个控制器横竖屏显示以及旋转屏控制技巧,附带iOS8以后显示电池状态栏
一.在应用中从竖屏模式强制转换为横屏模式 第一种方法:通过模态弹出视图的方式,使得特定ViewController坚持特定的interfaceOrientation(1)iOS6之后提供了这样一个方法 ...
- iOS里面消除使用代理调用方法时间警告问题
iOS里面有三种调用函数的方式: 直接调用方法 [对象名 方法]; performselector: [对象名 perform方法]; NSInvocation 调用 在使用代理调用 ...
- 115个Java面试题和答案——终极列表
from http://www.importnew.com/10980.html#collection http://www.importnew.com/11028.html 下面的章节分为上下两篇, ...
- 『重构--改善既有代码的设计』读书笔记----Inline Class
如果某个类没有做太多的事情,你可以将这个类的所有特性搬移到另外一个类中,然后删除原类.可以看到,Inline Class正好和Extract Class相反,后者是将一个巨类分解成多个小类从而来分担责 ...
- 与 空格的区别
nbsp 是 Non-Breaking SPace的缩写,即“不被折断的空格”,当两个单词使用 连接时,这两个单词就不会被分隔为2行,如下面 <div id="div1" ...
- NewRowNeeded和UserAddedRow事件以及RowsAdded的区别使用
NewRowNeeded事件当 VirtualMode 属性为 true 时,将在用户定位到 DataGridView 底部的新行时发生,适合给新行建立一些默认数据和按规则应该产生的数据,但此时不推荐 ...
- error_reporting()函数用法
首先要知道error_reporting()函数是用来设置错误级别并返回当前级别的.它有14个错误级别,如下: 1 E_ERROR 致命的运行时错误. 错误无法恢复过来 ...
- 使用Raphael 画图(二) 扩展的图形 (javascript)
看这文章前,建议先看第一编文章<使用Raphael 画图(一) 基本图形 (javascript)>. 在Raphael基础上扩展的图形: 要运行该例子要引入附件的2个js包.(g.rap ...
- Python【第七篇】面向对象进阶
大纲 一.面向对象高级语法 1.静态方法.类方法.属性方法 2.类的特殊成员方法 3.反射 二.异常处理 三.网络编程之socket基础 一.面向对象高级语法 1.静态方法:名义上归类管理,实际上静态 ...