CodeForces - 762B(贪心)
2017-02-16 22:36
507 查看
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis!
The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options.
You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once.
You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy.
Input
The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 105) — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively.
The next line contains one integer m (0 ≤ m ≤ 3·105) — the number of mouses in the price list.
The next m lines each describe another mouse. The i-th line contains first integer vali (1 ≤ vali ≤ 109) — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in.
Output
Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy.
Example
Input
2 1 1
4
5 USB
6 PS/2
3 PS/2
7 PS/2
Output
3 14
Note
In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
一部分电脑只能用usb的一部分电脑只能用PS/2的,还有一部分电脑都能用,先排序后贪心,先处理非通用的,然后处理通用的。
The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options.
You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once.
You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy.
Input
The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 105) — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively.
The next line contains one integer m (0 ≤ m ≤ 3·105) — the number of mouses in the price list.
The next m lines each describe another mouse. The i-th line contains first integer vali (1 ≤ vali ≤ 109) — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in.
Output
Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy.
Example
Input
2 1 1
4
5 USB
6 PS/2
3 PS/2
7 PS/2
Output
3 14
Note
In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
一部分电脑只能用usb的一部分电脑只能用PS/2的,还有一部分电脑都能用,先排序后贪心,先处理非通用的,然后处理通用的。
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> #include<string> using namespace std; int a,b,c,m; int pa[300005],pb[300005]; int main(){ string str; int num; scanf("%d%d%d",&a,&b,&c); int numa=0,numb=0; scanf("%d",&m); for(int i=0;i<m;i++){ cin>>num>>str; if(str=="USB"){pa[numa++]=num;} else{pb[numb++]=num;} } sort(pa,pa+numa); sort(pb,pb+numb); int i,j; long long ans=0,money=0; for(i=0;i<numa&&i<a;i++){ money+=pa[i];ans++; } for(j=0;j<numb&&j<b;j++){ money+=pb[j];ans++; } int cnt=0; while((i<numa||j<numb)&&cnt<c){ if(i==numa){money+=pb[j++];ans++;cnt++;} else if(j==numb){money+=pa[i++];ans++;cnt++;} else{ if(pa[i]<pb[j]){money+=pa[i++];ans++;cnt++;} else{money+=pb[j++];ans++;cnt++;} } } cout<<ans<<' '<<money; return 0; }
相关文章推荐
- Codeforces 651A Joysticks【贪心】
- CodeForces 732D Exams 二分+贪心
- CodeForces 137C【贪心+优先队列】
- 【Codeforces 贪心】Far Relative’s Problem
- 【Codeforces 733D】 Kostya the Sculptor【贪心】
- CodeForces 698A —Vacations(贪心)
- CodeForces 288A--贪心
- Codeforces 401A Vanya and Card【贪心】水题
- Codeforces 509c Sums of Digits 贪心
- 【Codeforces】899 C. Dividing the numbers 贪心
- codeforces-796C-Bank Hacking(贪心)
- CodeForces 3A Shortest path of the king(贪心)——Codeforces Beta Round #3
- Codeforces 337C Quiz【贪心+快速幂】
- Codeforces 452D [模拟][贪心]
- Codeforces 425A Sereja and Swaps(暴力+贪心)
- codeforces 659C (贪心 水~)
- CodeForces 588A. Duff and Meat 【贪心 dp】
- Codeforces 377B Preparing for the Contest【二分查找+优先队列+贪心】
- Codeforces 344C Rational Resistance【思维+贪心】
- CodeForces 487B RMQ贪心