Codeforces Round #653 (Div. 3) 參與排名人數11687
[codeforces 1374E1] Reading Books (easy version) 巧妙配對
總目錄詳見https://blog.csdn.net/mrcrack/article/details/103564004
在線測評地址http://codeforces.com/contest/1374/problem/E1
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
E1 - Reading Books (easy version) | GNU C++17 | Accepted | 124 ms | 6000 KB |
題目大意:給定一堆書,有隻有Alice喜歡的,有隻有Bob喜歡的,也有兩者都喜歡的,選出Alice喜歡的k本書,Bob喜歡的k本書,兩種選擇可以有重複,組成一個新集合,要求新集合對應的總閱讀時間最少,輸出這個最少時間。
基本思路:按閱讀時間,自小到大,將只有Alice喜歡的排序,將只有Bob喜歡的排序,將兩者自小到大配對,配成兩者都喜歡的,即兩本書的閱讀時間,合併成一本書的閱讀時間,歸類爲兩者都喜歡的書。
證明:只有Alice喜歡的書,與只有Bob喜歡的數量是一致的。
都喜歡的書有c本,只有Alice喜歡的有a本,只有Bob喜歡的有b本。
因Alice喜歡的書有k本,k=c+a;
因Bob喜歡的書有k本,k=c+b;
c+a=c+b
a=b.
樣例分析如下:
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
18
1 0 1
1 0 1
4 0 1
1 1 1
2 1 1
7 1 1
8 1 1
被選中的書:
1 1 1
2 1 1
7 1 1
8 1 1
1+2+7+8=18
總耗時18
AC代碼如下:
#include <cstdio>
#include <algorithm>
#define maxn 200010
using namespace std;
int a[maxn],b[maxn],c[maxn],an,bn,cn;
int main(){
int n,k,i,t,x,y,ans=0;
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++){
scanf("%d%d%d",&t,&x,&y);
if(x==1&&y==1)c[++cn]=t;
else if(x==1)a[++an]=t;
else if(y==1)b[++bn]=t;
}
sort(a+1,a+1+an);
sort(b+1,b+1+bn);
n=min(an,bn);
for(i=1;i<=n;i++)
c[++cn]=a[i]+b[i];//兩本各自喜歡的書合成一本大家都喜歡的書
sort(c+1,c+1+cn);
if(cn<k) printf("-1\n");
else{
for(i=1;i<=k;i++)ans+=c[i];
printf("%d\n",ans);
}
return 0;
}
收穫:main函數中,若返回值是1,即return 1;(本應是return 0;),測評系統會報錯Runtime error on test 3。