VJ STL专题例题与作业 M - 练2

VJ STL专题例题与作业
M - 练2
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.
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 10 9, 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

Time Limit Exceeded

#include<iostream>
#include <algorithm>
using namespace std;

struct point{
	int x;
	int y;
};
bool cmp(point a,point b){
	if(a.x!=b.x){
		return a.x<b.x;
	}
	else{
		return a.y<b.y;
	}
}

int main()
{
	int id=0;
	int n,m;
	while(cin>>n>>m){
		id++;
		if((n==0&&m==0)||id>50){
			break;
		}
		int out[m];
		point xy[n];
		for(int i=0;i<n;i++){
			cin>>xy[i].x>>xy[i].y;
		}
		sort(xy,xy+n,cmp);
		int err=xy[0].x-1;
		for(int i=0;i<m;i++){
			int mark;
			int ta;
			cin>>mark>>ta;
			int co=0;
			if(mark==0){//x
				for(int j=0;j<n;j++){
					if(xy[j].x==ta){
						xy[j].x=err;
						co++;
					}
				}
			}
			else{
				for(int j=0;j<n;j++){
					if(xy[j].y==ta && xy[j].x!=err){
						xy[j].x=err;
						co++;
					}
				}
			}
			out[i]=co;
		}
		for(int i=0;i<m;i++){
			cout<<out[i]<<endl;
		}
		cout<<endl;
	}
}

Accepted

#include<iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

int main()
{
	int id=0;
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF){
		id++;
		if((n==0&&m==0)||id>50){
			break;
		}
		int out[m];
		map<int,multiset<int> > xy;
		map<int,multiset<int> > yx;
		for(int i=0;i<n;i++){
			int x,y;
			scanf("%d %d",&x,&y);
			xy[x].insert(y);
			yx[y].insert(x);
		}
		for(int i=0;i<m;i++){
			multiset<int>::iterator it;
			int mark,site;
			scanf("%d %d",&mark,&site);
			if(mark==0){
				printf("%d\n",xy[site].size());
				for(it=xy[site].begin();it!=xy[site].end();it++){
					yx[*it].erase(site);
				}
				xy[site].clear();
			}
			else{
				printf("%d\n",yx[site].size());
				for(it=yx[site].begin();it!=yx[site].end();it++){
					xy[*it].erase(site);
				}
				yx[site].clear();
			}
		}
//		for(int i=0;i<m;i++){
//			cout<<out[i]<<endl;
//		}
		cout<<endl;
	}
}
VJ STL专题例题与作业 M - 练2VJ STL专题例题与作业 M - 练2 RY_zeze 发布了17 篇原创文章 · 获赞 0 · 访问量 80 私信 关注
上一篇:字符:循环左移


下一篇:D Strange Definition 质因素分解+思维