首页 > 编程语言 > 详细

Codeforces 496E. Distributing Parts 排序+贪心

时间:2015-03-24 23:00:42      阅读:334      评论:0      收藏:0      [点我收藏+]


将歌曲按低音从小到大排序,歌手也按低音从小到大排序.

对每首歌曲,在一个SET中查找低音满足条件的而高音刚好满足条件的歌手

E. Distributing Parts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are an assistant director in a new musical play. The play consists of n musical parts, each part must be performed by exactly one actor. After the casting the director chose m actors who can take part in the play. Your task is to assign the parts to actors. However, there are several limitations.

First, each actor has a certain voice range and there are some parts that he cannot sing. Formally, there are two integers for each actor, ciand di (ci?≤?di) — the pitch of the lowest and the highest note that the actor can sing. There also are two integers for each part — aj and bj(aj?≤?bj) — the pitch of the lowest and the highest notes that are present in the part. The i-th actor can perform the j-th part if and only if ci?≤?aj?≤?bj?≤?di, i.e. each note of the part is in the actor‘s voice range.

According to the contract, the i-th actor can perform at most ki parts. Besides, you are allowed not to give any part to some actors (then they take part in crowd scenes).

The rehearsal starts in two hours and you need to do the assignment quickly!

Input

The first line contains a single integer n — the number of parts in the play (1?≤?n?≤?105).

Next n lines contain two space-separated integers each, aj and bj — the range of notes for the j-th part (1?≤?aj?≤?bj?≤?109).

The next line contains a single integer m — the number of actors (1?≤?m?≤?105).

Next m lines contain three space-separated integers each, cidi and ki — the range of the i-th actor and the number of parts that he can perform (1?≤?ci?≤?di?≤?1091?≤?ki?≤?109).

Output

If there is an assignment that meets all the criteria aboce, print a single word "YES" (without the quotes) in the first line.

In the next line print n space-separated integers. The i-th integer should be the number of the actor who should perform the i-th part. If there are multiple correct assignments, print any of them.

If there is no correct assignment, print a single word "NO" (without the quotes).

Sample test(s)
input
3
1 3
2 4
3 5
2
1 4 2
2 5 1
output
YES
1 1 2
input
3
1 3
2 4
3 5
2
1 3 2
2 5 1
output
NO



/* ***********************************************
Author        :CKboss
Created Time  :2015年03月24日 星期二 18时04分40秒
File Name     :CF496E.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef pair<int,int> pII;

const int maxn=100100;

int n,m;

struct Part
{
	int l,r,id;
}part[maxn];

struct Actor
{
	int l,r,k,id;
}actor[maxn];

bool cmp1(Part A,Part B)
{
	if(A.l!=B.l) return A.l<B.l;
	return A.r<B.r;
}

bool cmp2(Actor A,Actor B)
{
	if(A.l!=B.l) return A.l<B.l;
	return A.r<B.r;
}

set<pII> SP;

int ans[maxn];
int num[maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	scanf("%d",&n);

	for(int i=0;i<n;i++)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		part[i].l=a; part[i].r=b; part[i].id=i;
	}

	scanf("%d",&m);
	for(int i=0;i<m;i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		actor[i].l=a; actor[i].r=b; actor[i].k=c; actor[i].id=i;
		num[i]=c;
	}

	sort(part,part+n,cmp1);
	sort(actor,actor+m,cmp2);

	bool flag=true;
	int nx=0;

	SP.insert(make_pair(1756942789,0));

	for(int i=0;i<n;i++)
	{
		/// add actor
		while(nx<m&&actor[nx].l<=part[i].l)
		{
			SP.insert(make_pair(actor[nx].r,actor[nx].id));
			nx++;
		}
		/// binary search
		pII temp=*(SP.lower_bound(make_pair(part[i].r,0)));
		if(temp.first==1756942789)
		{
			flag=false;
			break;
		}
		ans[part[i].id]=temp.second;
		num[temp.second]--;
		if(num[temp.second]==0) SP.erase(temp);
	}
	
	if(flag==false) puts("NO");
	else
	{
		puts("YES");
		for(int i=0;i<n;i++) printf("%d ",ans[i]+1);
		putchar(10);
	}
    
    return 0;
}



Codeforces 496E. Distributing Parts 排序+贪心

原文:http://blog.csdn.net/ck_boss/article/details/44600865

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!