A student’s life is fraught with complications. Some Berland University students know this only too well. Having studied for two years, they contracted strong antipathy towards the chairperson of some department. Indeed, the person in question wasn’t the kindest of ladies to begin with: prone to reforming groups, banning automatic passes and other mean deeds. At last the students decided that she just can’t get away with all this anymore…
The students pulled some strings on the higher levels and learned that the next University directors’ meeting is going to discuss n orders about the chairperson and accept exactly p of them. There are two values assigned to each order: ai is the number of the chairperson’s hairs that turn grey if she obeys the order and bi — the displeasement of the directors if the order isn’t obeyed. The students may make the directors pass any p orders chosen by them. The students know that the chairperson will obey exactly k out of these p orders. She will pick the orders to obey in the way that minimizes first, the directors’ displeasement and second, the number of hairs on her head that turn grey.
The students want to choose p orders in the way that maximizes the number of hairs on the chairperson’s head that turn grey. If there are multiple ways to accept the orders, then the students are keen on maximizing the directors’ displeasement with the chairperson’s actions. Help them.
Input
The first line contains three integers n (1 ≤ n ≤ 105), p (1 ≤ p ≤ n), k (1 ≤ k ≤ p) — the number of orders the directors are going to discuss, the number of orders to pass and the number of orders to be obeyed by the chairperson, correspondingly. Each of the following n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109), describing the corresponding order.
Output
Print in an arbitrary order p distinct integers — the numbers of the orders to accept so that the students could carry out the revenge. The orders are indexed from 1 to n in the order they occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
5 3 2
5 6
5 8
1 3
4 3
4 11
Output
3 1 2
Input
5 3 3
10 18
18 17
10 20
20 18
20 18
Output
2 4 5
题目描述:
有n个命令,要通过p个,某主席要在通过的p个中选择k个接受。
每个任务有两个值ai,bi, ai表示如果该主席接受该命令,她的头发变灰的数量,bi表示如果该主席不接受该命令时,议员不高兴值。
对于通过的p个命令,该主席要使议员的不高兴值和最小,在相同的情况下,要使自己的头发变灰的数量尽可能的少。
让你求出通过哪p个命令,使得该主席的头发变灰的数量最多,在相同的情况下,输出使议员不高兴最大的选择。
题目分析:
这道题需要多种排序方法。
首先须知道,对于每一个选择(p命令),该主席一定是把b值最小的p-k个不接受,如果b有相同的,则尽可能使自己的头发变灰的数量最小(即放弃命令的a值尽量大)。
1.按 b:小->大 a:大->小 排序,前p-k个无论a有多大,如果选它都没用(肯定会被放弃),a不能发挥作用,所以只能在后面的n-(p-k)个里选。
2.按 a: 大->小 b:大->小 排序,选择前k个,使得a的和最大,b的和最小值也最大(因为要保证主席需要接受它)。
3.保证了k个命令a的和最大后,剩下的任务就是保证p-k个命令b的和最大了。再次像1中那样排序,选出p-k个主席肯定不会通过的(即b很小的)。
代码如下:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=1e5+5;
struct Node{
int a,b;
int id,st;
}a[N];
bool cmp1(const Node &x,const Node &y)
{ //以b位第一关键字,a为第二关键字,st为第三关键字进行排序
if(x.b!=y.b) return x.b<y.b;
if(x.a!=y.a) return x.a>y.a;
return x.st<y.st;
}
bool cmp2(const Node &x,const Node &y)
{ //以a为第一关键字,b为第二关键字进行排序
if(x.a!=y.a) return x.a>y.a;
return x.b>y.b;
}
bool vis[N];
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n,p,k;
cin>>n>>p>>k;
for(int i=1;i<=n;i++) //初始化
{
cin>>a[i].a>>a[i].b;
a[i].id=i;
a[i].st=0;
}
sort(a+1,a+1+n,cmp1); //用第一种方法排序
for(int i=1;i<=p-k;i++) //选出p-k个肯定不会被接受的
a[i].st=1;
sort(a+1,a+1+n,cmp2); //用第二种方法排序
int cnt=0;
for(int i=1;cnt<k;i++) //选出k个肯定会被接受的
if(!a[i].st)
{
cnt++;
a[i].st=2;
vis[a[i].id]=true; //用vis记录这k个命令的id
cout<<a[i].id<<" ";
}
sort(a+1,a+1+n,cmp1);
int num=0;
cnt=0;
for(int i=n;num<p-k;i--) //选出p-k个肯定不会被通过的
{//将之前选出的k个找到,在这k个后面选p-k个肯定不会被通过的命令
if(cnt>=k) //即这p-k个的b肯定小于k个的b
{
num++;
cout<<a[i].id<<" ";
}
if(vis[a[i].id]) cnt++;
}
cout<<endl;
return 0;
}