2019年我能變強組隊訓練賽第十場 C Criss-Cross Cables(優先隊列模擬)

問題 C: Criss-Cross Cables

時間限制: 2 Sec  內存限制: 128 MB
提交: 72  解決: 18
[提交] [狀態] [命題人:admin]

題目描述

As a participant in the BAPC (Bizarrely Awful Parties Competition) you are preparing for your next show. Now,you do not know anything about music, so you rip off someone else’s playlist and decide not to worry about that any more. What you do worry about, though, is the aesthetics of your set-up: if it looks too simple, people will be unimpressed and they might figure out that you are actually a worthless DJ.
It doesn’t take you long to come up with a correct and fast solution to this problem. You add a long strip with a couple of useless ports, and add some useless cables between these ports. Each of these cables connects two ports, and these special ports can be used more than once. Everyone looking at the massive tangle of wires will surely be in awe of your awesome DJ skills.
However, you do not want to connect the same two ports twice directly. If someone notices this, then they will immediately see that you are a fraud!
You’ve made a large strip, with the ports in certain fixed places, and you’ve found a set of cables with certain lengths that you find aesthetically pleasing. When you start trying to connect the cables, you run into another problem. If the cables are too short, you cannot use them to connect the ports! So you ask yourself the question whether you’re able to fit all of the cords onto the strip or not. If not, the aesthetics are ruined, and you’ll have to start all over again.

 

輸入

The first line has 2 ≤ n ≤ 5 · 105 and 1 ≤ m ≤ 5 · 105, the number of ports on the strip and the number of wires.
• The second line has integers 0 ≤ x1 < · · · < xn ≤ 109, the positions of the n sockets.
• The third line has m integers l1, . . . , lm, the lengths of the wires, with 1 ≤ li ≤ 109.

 

輸出

Print yes if it is possible to plug in all the wires, or no if this is not possible.

 

樣例輸入

複製樣例數據

4 4
0 2 3 7
1 3 3 7

樣例輸出

yes

題意:給出n個接線柱的座標,m個線的長度,問是否能把所有的線都接上,條件是:不能有多根線接在相同的兩根上,比如說:1號線接1 2柱,其他的線就不能接1 2柱了,當然一條線只能接2個接線柱。

題解:如果把所有的距離都放進優先隊列裏面,肯定會T,因爲找距離就是O(n^2),所以我們考慮先把x_{i+1} -x_{i} 放到隊列裏面,把線的長度排序,看看最小的,與優先隊列裏面最小的是否匹配,如果匹配,pop掉,假設pop的兩個的位置是i,j那麼再往隊列裏面加x_{j+1}-x_{i}x_{j}-x_{i-1},如果不匹配直接輸出“no”,因爲不會有更小的距離了。注意標記兩個接線柱之間只能用一次。

注意:可能是我的寫法問題,用long long會超內存,改成int,用map標記會T,改成unordered_map標記

上代碼:

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <tr1/unordered_map>
using namespace std;
using namespace tr1;
const int MAX = 5e5+100;
struct hh{
	int l,r,w;
	bool operator < (const hh& b) const
	{
		return w>b.w;
	}
}tmp;
priority_queue<hh> q;
int b[MAX];
int c[MAX];
unordered_map<int,unordered_map<int,int> > mp;
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i = 0; i < n;i++){
		scanf("%d",&c[i]);
	}
	for (int i = 0; i < m;i++){
		scanf("%d",&b[i]);
	}
	for (int i = 0; i < n-1;i++){
		tmp.l=i;
		tmp.r=i+1;
		tmp.w=c[i+1]-c[i];
		q.push(tmp);
	}
	sort(b,b+m);
	int cnt=0;
	while(!q.empty()){
		hh fuck=q.top();
		q.pop();
		//cout << fuck.w << " " << fuck.l << " " << fuck.r  << endl;
		if(b[cnt]>=fuck.w){
			cnt++;
			int l=fuck.l;
			int r=fuck.r;
			tmp.l=l-1;
			tmp.r=r;
			if(tmp.l>=0&&mp[tmp.l][tmp.r]==0){
				mp[tmp.l][tmp.r]=1;
				tmp.w=c[tmp.r]-c[tmp.l];
				q.push(tmp);
			//	cout << tmp.w << " " << tmp.l << " " << tmp.r << "*" << endl;
			}
			tmp.l=l;
			tmp.r=r+1;
			if(tmp.r<n&&mp[tmp.l][tmp.r]==0){
				mp[tmp.l][tmp.r]=1;
				tmp.w=c[tmp.r]-c[tmp.l];
				q.push(tmp);
				//cout << tmp.w << " " << tmp.l << " " << tmp.r << "&" << endl;
			}
		}
		else{
			if(cnt<m){
				puts("no");
				return 0;
			}
		}
		if(cnt==m) break;
	}
	if(cnt==m) puts("yes");
	else puts("no");
	return 0;
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章