小奇画画
时间限制: 1 Sec 内存限制: 128 MB
题目描述
红莲清泪两行欲吐半点却无
如初是你杳然若绯雾还在水榭畔画楼处
是谁衣白衫如初谁红裳如故
——《忆红莲》
小奇想画几朵红莲,可惜它刚开始学画画,只能从画圆开始。小奇画了n个圆,它们的圆心都在x轴上,且两两不相交(可以相切)。现在小奇想知道,它画的圆把画纸分割成了多少块?(假设画纸无限大)
输入
第一行包括1个整数n。
接下来n行,每行两个整数x,r,表示小奇画了圆心在(x,0),半径为r的一个圆。
输出
输出一个整数表示答案。
样例输入
4
7 5
-9 11
11 9
0 20
样例输出
6
思路
画纸一开始为1块,每个圆被画下时,一定会将画纸至少多分割成一块,如果三个圆同时相切,那么还会再多分割一块出来。
例如1,2,4或者1,2,3

由于程序中对map的随机存储较多,使用 unordered_map (HashMap)代替 map 可以大大加快运行速度。
c++11以下版本unordered_map使用
#include <tr1/unordered_map>
using namespace std::tr1;
#include <iostream>
#include <tr1/unordered_map>
#include <algorithm>
#define ll long long
using namespace std;
using namespace std::tr1;
int n, ans = 1;
ll x, r;
pair<ll, ll> in[300001];
unordered_map<ll, bool> ma;
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> in[i].first >> in[i].second;
sort(in, in + n, greater<pair<ll, ll> >());
for (int i = 0; i < n; i++) {
x = in[i].first; //圆心坐标
r = in[i].second; //半径
ans++;
//画纸一开始为1块
//每个圆被画下时,一定会将画纸至少多分割成一块
//如果三圆同时相切,那么还会再多分割一块出来。
if (ma[x + r] && ma[x - r])
ans++;
else
ma[x + r] = ma[x - r] = true;
}
cout << ans;
return 0;
}
377

被折叠的 条评论
为什么被折叠?



