Problem C. ICPC Giveaways
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100500/attachments
Description
During the preparation for the ICPC contest, the organizers prepare bags full of giveaways for the contestants. Each bag usually contains an MP3 Player, a Sim Card, a USB HUB, a USB Flash Drive, ... etc. A problem happened during the delivery of the components of the bags, so not every component was delivered completely to the organizers. For example the organizers ordered 10 items of 4 different types, and what was delivered was 7, 6, 8, 9 from each type respectively. The organizers decided to form bags anyway, but they have to abide by 2 rules. All formed bags should have exactly the same items, and no bag should contain 2 items of the same type (either 1 or 0). Knowing that each item has an amusement value (for sure an MP3 Player is much more amusing than a Sim Card), the organizers decided to get the max possible total amusement. The total amusement is the amusement value of a single bag multiplied by the number of bags. Note that not every contestant should receive a bag. The amusement value of each item type is calculated using this equation:(i × i) mod C where i is an integer that represents the item type, and C is a value that will be given as an input. Please help the ICPC organizers to determine what the maximum total amusement is.
Input
T is the number of test cases. For each test case there will be 3 integers M,N and C, where M is the number of items, N is the total number of types, and C is as described above then M integer representing the type of each item. 1 ≤ T ≤ 100 1 ≤ M ≤ 10, 000 1 ≤ N ≤ 10, 000 1 ≤ C ≤ 10, 000 1 ≤ itemi ≤ N
Output
For each test case print a single line containing: Case_x:_y x is the case number starting from 1. y is the required answer. Replace the underscores with spaces.
Sample Input
1 10 3 9 1 1 2 2 1 1 2 3 1 2
Sample Output
Case 1: 20
HINT
题意
要求从已有的物品中选取若干个,组成若干个一模一样的bag,要求每个bag中同类物品只有一个,最后选手满意度=bag数目*每个bag满意度
题解:
满意度=sum(value(k))*bag,k满足cnt[k]>=bag,那么只要排序,维护前缀和,然后扫描一遍数组即可
//debug:1.一开始就没有注意到mod c是针对每一种物品的而不是针对总和的
代码:
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <ctime> 5 #include <iostream> 6 #include <algorithm> 7 #include <set> 8 #include <vector> 9 #include <sstream> 10 #include <queue> 11 #include <typeinfo> 12 #include <fstream> 13 #include <map> 14 #include <stack> 15 typedef long long ll; 16 using namespace std; 17 #define test freopen("1.txt","r",stdin) 18 #define maxn 2000001 19 #define mod 10007 20 #define eps 1e-9 21 const int inf=0x3f3f3f3f; 22 const ll infll = 0x3f3f3f3f3f3f3f3fLL; 23 inline int read() 24 { 25 ll x=0,f=1; 26 char ch=getchar(); 27 while(ch<‘0‘||ch>‘9‘) 28 { 29 if(ch==‘-‘)f=-1; 30 ch=getchar(); 31 } 32 while(ch>=‘0‘&&ch<=‘9‘) 33 { 34 x=x*10+ch-‘0‘; 35 ch=getchar(); 36 } 37 return x*f; 38 } 39 inline void out(int x) 40 { 41 if(x>9) out(x/10); 42 putchar(x%10+‘0‘); 43 } 44 //************************************************************************************** 45 struct item{ 46 int v; 47 int cnt; 48 }item[10000+100]; 49 bool cmp(struct item a,struct item b) 50 { 51 return a.cnt>b.cnt; 52 } 53 int sum[10000+100]; 54 int main() 55 { 56 int t=read(); 57 for(int cc=1;cc<=t;cc++) 58 { 59 int m=read(),n=read(),c=read(); 60 memset(sum,0,sizeof(sum)); 61 for(int i=1;i<=n;i++){ 62 item[i].cnt=0; 63 item[i].v=i*i%c; 64 } 65 for(int i=1;i<=m;i++){ 66 int type=read(); 67 item[type].cnt++; 68 } 69 sort(item+1,item+n+1,cmp); 70 for(int i=1;i<=n;i++){ 71 if(i==1) sum[i]=item[i].v; 72 else sum[i]=sum[i-1]+item[i].v; 73 } 74 int maxx=-1; 75 for(int i=1;i<=n;i++){ 76 maxx=max(maxx,item[i].cnt*sum[i]); 77 } 78 printf("Case %d: %d\n",cc,maxx); 79 } 80 return 0; 81 }
另外,以c为标准维护前缀和不知道为什么错。。。
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; #define test freopen("1.txt","r",stdin) #define maxn 2000001 #define mod 10007 #define eps 1e-9 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline int read() { ll x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘)f=-1; ch=getchar(); } while(ch>=‘0‘&&ch<=‘9‘) { x=x*10+ch-‘0‘; ch=getchar(); } return x*f; } inline void out(int x) { if(x>9) out(x/10); putchar(x%10+‘0‘); } //************************************************************************************** struct item{ int v; int cnt; }item[10000+100]; bool cmp(struct item a,struct item b) { return a.cnt>b.cnt; } int sum[10000+100]; int main() { int t=read(); for(int cc=1;cc<=t;cc++) { int m=read(),n=read(),c=read(); memset(sum,0,sizeof(sum)); for(int i=0;i<c;i++){ item[i].cnt=0; item[i].v=i; } for(int i=1;i<=m;i++){ int type=read(); int v=type*type%c; item[v].cnt++; } sort(item,item+c,cmp); for(int i=0;i<c;i++){ if(i==0) sum[i]=item[i].v; else sum[i]=sum[i-1]+item[i].v; } int maxx=-1; for(int i=0;i<c;i++){ // printf("%d\n",item[i].cnt); maxx=max(maxx,item[i].cnt*sum[i]); } printf("Case %d: %d\n",cc,maxx); } return 0; }