The Happy Worm 分类: POJ 排序 2015-08-03 18:57 5人阅读 评论(0) 收藏
The Happy Worm
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 4698 Accepted: 1047
Description
The Happy Worm lives in an m*n rectangular field. There are k stones placed in certain locations of the field. (Each square of the field is either empty, or contains a stone.) Whenever the worm sleeps, it lies either horizontally or vertically, and stretches so that its length increases as much as possible. The worm will not go in a square with a stone or out of the field. The happy worm can not be shorter than 2 squares.
The question you are to answer is how many different positions this worm could be in while sleeping.
Input
The first line of the input contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. The first line of each test case contains three integers m, n, and k (1 <= m,n,k <= 131072). The input for this test case will be followed by k lines. Each line contains two integers which specify the row and column of a stone. No stone will be given twice.
Output
There should be one line per test case containing the number of positions the happy worm can be in.
Sample Input
1
5 5 6
1 5
2 3
2 4
4 2
4 3
5 1
Sample Output
9
题意大致理解,不过在实现的过程中却又不少的疑问,希望大神们给指点一下。
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define WW freopen("output.txt","w",stdout)
#define RR freopen("input.txt","r",stdin)
using namespace std;
const int MAX= 140010 ;
struct point
{
int x;
int y;
}a[MAX];
int sum;
bool cmp1(point a,point b)
{
if(a.x<b.x||(a.x==b.x&&a.y<b.y))
{
return true;
}
return false;
}
bool cmp2(point a,point b)
{
if(a.y<b.y||(a.y==b.y&&a.x<b.x))
{
return true;
}
return false;
}
int main()
{
int T;
int n,m,k;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d",&n,&m,&k);
for(int i=1;i<=k;i++)
{
scanf("%d %d",&a[i].x,&a[i].y);
}
sum=0;
a[0].x=1;
a[0].y=0;
a[k+1].x=n;
a[k+1].y=m+1;
sort(a+1,a+k+1,cmp1);
for(int i=1;i<=k+1;i++)
{
if(a[i].x==a[i-1].x)
{
if(a[i].y-a[i-1].y>2)
{
sum++;
}
}
else
{
sum+=(a[i].x-a[i-1].x-1);
if(m-a[i-1].y>=2)//这里为什么是>=,而不是>
{
sum++;
}
if(a[i].y-1>=2)
{
sum++;
}
}
}
a[0].x=0;
a[0].y=1;
a[k+1].x=n+1;
a[k+1].y=m;
sort(a+1,a+k+1,cmp2);
for(int i=1;i<=k+1;i++)
{
if(a[i].y==a[i-1].y)
{
if(a[i].x-a[i-1].x>2)
{
sum++;
}
}
else
{
sum+=(a[i].y-a[i-1].y-1);
if(n-a[i-1].x>=2)//这里为什么是>=,而不是>
{
sum++;
}
if(a[i].x-1>=2)
{
sum++;
}
}
}
printf("%d\n",sum);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
The Happy Worm 分类: POJ 排序 2015-08-03 18:57 5人阅读 评论(0) 收藏的更多相关文章
- LightOJ1002 分类: 比赛 最短路 2015-08-08 15:57 15人阅读 评论(0) 收藏
I am going to my home. There are many cities and many bi-directional roads between them. The cities ...
- PC机安装Qt以及QT交叉编译环境 分类: OpenCV ZedBoard shell ubuntu Eye_Detection 2014-11-08 18:57 246人阅读 评论(0) 收藏
PC: apt-get install qtcreator Qt Embedded for ZedBoard: 下载qt-everywhere-opensource-src-4.7.3.tar.gz, ...
- zedboard--交叉编译Opencv库的生成 分类: shell ubuntu fool_tree的笔记本 ZedBoard OpenCV 2014-11-08 18:57 171人阅读 评论(0) 收藏
Opencv的移植,xzyfeixiang和rainysky的博客. 第一步肯定是下载opencv的源码包 第二步已经做好的交叉编译环境. 第三步下载安装cmake apt-get install ...
- 摄像头参数查看与调节 分类: C/C++ OpenCV 2014-11-08 18:13 138人阅读 评论(0) 收藏
cvGetCaptureProperty 获得视频获取结构的属性 double cvGetCaptureProperty( CvCapture* capture, int property_id ); ...
- highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏
/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...
- const char*, char const* and char *const 分类: C/C++ OpenCV 2014-11-08 18:10 114人阅读 评论(0) 收藏
const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目. 事实上这个概念谁都有只是三种声明方式非常相似很容易记混. Bjarne在他的 ...
- 写在新建博客的第一天 分类: fool_tree的笔记本 2014-11-08 17:57 144人阅读 评论(0) 收藏
来CSDN开博客的目的有两个: 其一是因为CSDN的代码输出,看过一些博文,觉得这里的代码输出真的很漂亮: 其二则是因为,感觉自己印象笔记用久了之后,渐渐地几乎不再自己写些东西了,习惯了方便的剪藏插件 ...
- android开发之AlertDialog点击按钮之后不消失 分类: android 学习笔记 2015-07-15 18:07 89人阅读 评论(0) 收藏
最近有这样一个需求,我需要用户在一个弹出框里输入密码来验证,验证成功当然好说,但是如果验证失败则需要把alertdialog的标题改为"密码错误,请重新输入",并且这个alertd ...
- OC基础:数组.字典.集 分类: ios学习 OC 2015-06-18 18:58 47人阅读 评论(0) 收藏
==============NSArray(不可变数组)=========== NSArray,继承自NSObject 用来管理(储存)一些有序的对象,不可变数组. 创建一个空数组 NSArray ...
随机推荐
- html a标签链接使用action 参数传递中文乱码
<a href="queryByType?ptype=鼠标"> 在后台变量ptype接收的值为乱码 解决方法: 在tomcat的server.xml文件中添加 URIE ...
- hdu 2846 Repository
http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...
- Python学习总结19:类(一)
在Python中,可以通过class关键字定义自己的类,通过类私有方法“__init__”进行初始化.可以通过自定义的类对象类创建实例对象. class Student(object): count ...
- adb devices 显示error
1.adb kill-server 2.adb start-server
- C++新手之培养良好的编程风格
内功深厚的武林高手出招往往平淡无奇.同理,编程高手也不会用奇门怪招写程序.良好的编程风格是产生高质量程序的前提. 下面以C++为例,来给大家介绍. 一. 命名约定 有不少人编程时用拼音给函数或变量命名 ...
- 在TextView上加上下划线或中划线
在做商城项目是需要用到原价格以及降价后的价格,不可避免用到下划线或中划线 textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下 ...
- paper 49:论文退稿?审稿人帮你总结了22个能避免的常见问题
很多投稿出去的文章都是可上可下的.往往退稿的时候,审稿人提了一堆意见,说退稿.但是大家想过没有?如果能事先预测到这些意见,或者请懂行的人事先看过文章预测出意见,然后根据这些意见修改好了再投出去,说不定 ...
- OpenMP的调度
schedule(static, size) 这是静态调度,如果没有设置size, 默认是根据任务书来决定, 比如我电脑是8核,有26个任务,那么分配后结果是4 4 3 3 3 3 3 3. 因为先分 ...
- android 弹幕效果demo
记得之前有位朋友在我的公众号里问过我,像直播的那种弹幕功能该如何实现?如今直播行业确实是非常火爆啊,大大小小的公司都要涉足一下直播的领域,用斗鱼的话来讲,现在就是千播之战.而弹幕则无疑是直播功能当中最 ...
- Linux Kernel之flush_cache_all在ARM平台下是如何实现的【转】
转自:http://blog.csdn.net/u011461299/article/details/10199989 版权声明:本文为博主原创文章,未经博主允许不得转载. 在驱动程序的设计中,我们可 ...