5725: 小奇画画
时间限制: 1 Sec 内存限制: 128 MB
提交: 873 解决: 209
[提交] [状态] [讨论版] [命题人:admin]
题目描述
红莲清泪两行欲吐半点却无
如初是你杳然若绯雾还在水榭畔画楼处
是谁衣白衫如初谁红裳如故
——《忆红莲》
小奇想画几朵红莲,可惜它刚开始学画画,只能从画圆开始。小奇画了n个圆,它们的圆心都在x轴上,且两两不相交(可以相切)。现在小奇想知道,它画的圆把画纸分割成了多少块?(假设画纸无限大)
输入
第一行包括1个整数n。
接下来n行,每行两个整数x,r,表示小奇画了圆心在(x,0),半径为r的一个圆。
输出
输出一个整数表示答案。
样例输入
4 7 5 -9 11 11 9 0 20
样例输出
6
提示
对于 100%数据,1<=n<=300000,-10^9<=x<=10^9,1<=r<=10^9。
训练赛时没想出来这题,一碰到跟图有关的就自动挂掉,后来学了一个办法,两层循环,判断如果有大圆里包围几个小圆把该大圆上下分割成两块时,答案就加一,一开始ans=n+1
本来是A了,结果后来加数据重判,wa了
后来看大佬博客
大概思想还是和原来一样,但是这次用map来确定是否有重合,(这让一个STL勉强只会栈 队列的人瑟瑟发抖)
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx=3e5+100;
pair<ll,ll> s[maxx];
map<ll,bool> f;
int main()
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++){
scanf("%lld%lld",&s[i].first,&s[i].second);
}
sort(s+1,s+n+1,greater<pair<ll,ll> >());
int ans=1;
ll x,r;
for(int i=1; i<=n; i++){
x=s[i].first;
r=s[i].second;
ans++;
if(f[x-r] && f[x+r]) ans++;
else f[x-r]=f[x+r]=true;
}
printf("%d\n",ans);
return 0;
}