HDU 4022Bombing 2011網路賽(二分 或 STL)
Bombing
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 1884 Accepted Submission(s): 703
Problem Description
It’s a cruel war which killed millions of people and ruined series of cities. In order to stop it, let’s bomb the opponent’s base.
It seems not to be a hard work in circumstances of street battles, however, you’ll be encountered a much more difficult instance: recounting exploits of the military. In the bombing action, the commander will dispatch a group of bombers with weapons having the huge destructive power to destroy all the targets in a line. Thanks to the outstanding work of our spy, the positions of all opponents’ bases had been detected and marked on the map, consequently, the bombing plan will be sent to you.
Specifically, the map is expressed as a 2D-plane with some positions of enemy’s bases marked on. The bombers are dispatched orderly and each of them will bomb a vertical or horizontal line on the map. Then your commanded wants you to report that how many bases will be destroyed by each bomber. Notice that a ruined base will not be taken into account when calculating the exploits of later bombers.
It seems not to be a hard work in circumstances of street battles, however, you’ll be encountered a much more difficult instance: recounting exploits of the military. In the bombing action, the commander will dispatch a group of bombers with weapons having the huge destructive power to destroy all the targets in a line. Thanks to the outstanding work of our spy, the positions of all opponents’ bases had been detected and marked on the map, consequently, the bombing plan will be sent to you.
Specifically, the map is expressed as a 2D-plane with some positions of enemy’s bases marked on. The bombers are dispatched orderly and each of them will bomb a vertical or horizontal line on the map. Then your commanded wants you to report that how many bases will be destroyed by each bomber. Notice that a ruined base will not be taken into account when calculating the exploits of later bombers.
Input
Multiple test cases and each test cases starts with two non-negative integer N (N<=100,000) and M (M<=100,000) denoting the number of target bases and the number of scheduled bombers respectively. In the following N line, there is a pair of integers x and y
separated by single space indicating the coordinate of position of each opponent’s base. The following M lines describe the bombers, each of them contains two integers c and d where c is 0 or 1 and d is an integer with absolute value no more than 109,
if c = 0, then this bomber will bomb the line x = d, otherwise y = d. The input will end when N = M = 0 and the number of test cases is no more than 50.
Output
For each test case, output M lines, the ith line contains a single integer denoting the number of bases that were destroyed by the corresponding bomber in the input. Output a blank line after each test case.
Sample Input
3 2
1 2
1 3
2 3
0 1
1 3
0 0
Sample Output
2
1
Source
題目大意:題目意思說給你n個基地的座標位置,然後有m個炸彈,炸彈有兩種,0號炸彈可以炸燬x相同的基地,1號炸彈可以炸燬y相同的基地。問你每顆炸彈能炸燬的基地數目,如果這個基地已經被先前的炸彈炸燬了,就不能再被炸了。
解題思路:開始上來就直接二分,沒有考慮到座標的範圍,那樣的話是需要將座標位置離散化的。但是考慮到另一個問題,離散化將x座標離散化,但是把x炸掉之後,那所有x的基地都將被炸掉,所以最後在每一個點先設定一個索引index,然後index是多少,就表示哪個點,開始設定visi都為0,具體實現見程式碼:
題目地址:Bombing
二分AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#define maxx 100005
using namespace std;
int n,m;
struct node
{
int x;
int y;
int index; //每個點有自己的標記
}mq;
node a[maxx],b[maxx];
int visi[maxx];
//a按照x排序 b按照y排序
//visi記錄每個基地是否被炸
bool cmp1(node m1,node m2) //按照x排序
{
return m1.x<=m2.x;
}
bool cmp2(node m1,node m2) //按照x排序
{
return m1.y<=m2.y;
}
int cal1(int xx) //計算x相等的數目
{
int l,r,mid,ans;
l=0,r=n-1,mid=(l+r)>>1;
ans=0;
while(l<=r)
{
if(a[mid].x<xx) l=mid+1;
else r=mid-1;
mid=(l+r)>>1;
}
for(mid=l;mid<n;mid++)
{
if(a[mid].x!=xx) break;
if(visi[a[mid].index]) continue;
visi[a[mid].index]=1; //標記這個點的x座標被炸
ans++;
}
return ans;
}
int cal2(int yy) //計算y相等的數目
{
int l,r,mid,ans;
l=0,r=n-1,mid=(l+r)>>1;
ans=0;
while(l<=r)
{
if(b[mid].y<yy) l=mid+1;
else r=mid-1;
mid=(l+r)>>1;
}
//cout<<l<<" "<<r<<" "<<mid<<endl;
for(mid=l;mid<n;mid++)
{
if(b[mid].y!=yy) break;
if(visi[b[mid].index]) continue;
visi[b[mid].index]=1; //標記這個點的x座標被炸
ans++;
}
return ans;
}
int main()
{
int i,tmp,p;
while(scanf("%d%d",&n,&m))
{
if(n==0&&m==0)
break;
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].index=i;
b[i]=a[i];
}
memset(visi,0,sizeof(visi));
sort(a,a+n,cmp1);
sort(b,b+n,cmp2);
for(i=0;i<m;i++)
{
scanf("%d%d",&tmp,&p);
if(tmp==0) printf("%d\n",cal1(p));
else printf("%d\n",cal2(p));
}
puts("");
}
return 0;
}
//484MS
在網上搜了一下,很多都是用STL過的。
首先建立兩個map<int,multiset<int> > mx,注意後面兩個尖括號中間要有個空格。否則編譯錯誤,第一個map是x,第一維是x,第二維便是這個x對應的y。然後每次如果看能炸燬多少個基地很容易知道,直接mx[p].size(),即可求出,主要是找到個數之後要把基地炸燬,把x對應的y刪掉,把y對應的x也要刪掉,具體實現見程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define maxx 100005
using namespace std;
typedef map<int,multiset<int> > node;
node mx;
node my;
int cal(node &x,node &y,int p)
{
int res=x[p].size();
multiset<int>::iterator it;
for(it=x[p].begin();it!=x[p].end();it++)
{
//x[p].erase(*it); //不能這樣刪除,這樣刪之後it無效
y[*it].erase(p);
}
x[p].clear(); //直接一次清空
return res;
}
int main()
{
int n,m,x,y,q,p;
while(scanf("%d%d",&n,&m))
{
if(n==0&&m==0) break;
mx.clear(); my.clear();
while(n--)
{
scanf("%d%d",&x,&y);
mx[x].insert(y);
my[y].insert(x);
}
while(m--)
{
scanf("%d%d",&q,&p);
if(q==0) printf("%d\n",cal(mx,my,p));
else printf("%d\n",cal(my,mx,p));
}
puts("");
}
return 0;
}
//1062MS
相關文章
- 2014上海網路賽1004||hdu5045 二分圖的最佳匹配 或 狀態壓縮dp
- 2013杭州網路賽C題HDU 4640(模擬)
- 2014鞍山網路賽 E題||hdu 5001 概率dp
- hdu 4292 網路最大流
- hdu2011javaJava
- HDU5441 Travel (2015年長春網路賽,並查集)並查集
- 2014廣州網路賽1003||hdu 5024 搜尋
- hdu7462-字串【SAM,二分】字串
- hdu5452 || 瀋陽網路賽1003題 最近公共祖先問題
- 2014北京網路賽1007||hdu5038 模擬
- 2014西安網路賽1006||hdu5012 bfs
- HDU 3486 Interviewe(RMQ+二分)ViewMQ
- HDU 5884-Sort(佇列+二分)佇列
- hdu 4089||2011年北京現場賽I題 概率dp(公式不好推)公式
- 2014廣州網路賽1004||hdu5025 分層最短路
- 2014北京網路賽1006||hdu5037 思維題
- 2014西安網路賽1009||hdu5015 矩陣矩陣
- hdu4400 STL應用 查詢思維題
- 狀壓 + 網路流 -- Escape HDU - 3605
- 2014上海網路賽1004||hdu5045 contest【狀態壓縮dp】
- HDU 4279 2012網路賽Number(數論 尤拉函式結論約數個數)函式
- 2014西安網路賽1008||hdu5014 二進位制
- 網路流&費用流&二分圖
- 2020ICPC·小米 網路選拔賽第二場 Subsequence Pair(貪心二分)AI
- hdu5445 || 2015長春網路賽1009題 多重揹包問題
- 2.27 二分圖與網路流複習
- 2013成都網路賽1004題HDU 4731Minimum palindrome (思維題目)
- hdu4280 網路流+掛(無向圖的網路流注意建邊)
- HDU 4119Isabella's Message2011成都現場賽I題(字串模擬)字串
- HDU 6274 Master of Sequence(思維+樹狀陣列+二分)AST陣列
- hdu4751Divide Groups【判斷二分圖】IDE
- HDU-3461 Code Lock 並查集 + 二分求冪並查集
- HDU 4417-Super Mario(劃分樹-二分查詢)
- 2014廣州網路賽1002||hdu5023 線段樹&&狀態壓縮
- 2011網路儲存世界大會
- 中國網際網路或將被世界淘汰 !
- 網際網路金融或曇花一現
- 網路安全技能大賽首登網路安全周舞臺