codeforces Gym 100500C C. ICPC Giveaways 暴力

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;
}

时间: 2024-11-05 22:33:05

codeforces Gym 100500C C. ICPC Giveaways 暴力的相关文章

codeforces Gym 100500C C. ICPC Giveaways 排序

Problem C. ICPC GiveawaysTime 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 us

Codeforces Gym 100002 C &quot;Cricket Field&quot; 暴力

"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 Description Once upon a time there was a greedy King who ordered his chief Architect to build a field for royal cricket inside his park. The King was so

Codeforces Gym 100513M M. Variable Shadowing 暴力

M. Variable Shadowing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/problem/M Description In computer programming, variable shadowing occurs when a variable declared within a certain scope has the same name as a variab

Codeforces Gym 100513G G. FacePalm Accounting 暴力

G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/problem/G Description An owner of a small company FacePalm has recently learned that the city authorities plan to offer to small businesses to partic

codeforces Gym 100500C D.Hall of Fame 排序

Hall of Fame Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description It was the second day of IBM Chill Zone, and it was the time for distributing the prizes. Unfortunately due to unknown reasons, the org

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces 442B Kolya and Tandem Repeat(暴力)

题目连接:Codeforces 442B Kolya and Tandem Repeat 题目大意:给出一个字符串,可以再添加n个字符,问说可以找到SS的子串形式,S尽量长. 解题思路:枚举长度和起点判断即可,超过len的可以作为任意值,但是超过len+n就不行了. #include <cstdio> #include <cstring> const int N = 205; int n, len; char s[N]; bool judge (int l) { if (l <

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces Round #224 (Div. 2) D 暴力搜索加记忆化

题意读了半年,英语太渣,题意是摆两个棋子在棋盘上作为起点,但是起点不能在#上,然后按照图的指示开始走, < 左 > 右 ^上 v下,走的时候只能按照图的指示走,如果前方是 #的话,可以走进去,但是 走进去之后便不能再走了,走的途中两个棋子不能相碰,但是最终都走到同一个#里是没事的,并且若是能走 无限步的话 输出 -1, 例如  > < 这样左右左右的走就能无限走,然后问你 两个棋子走的最大步数的和 一开始被输出-1给困住了,因为除了 .> <这样以外  还可以刚好形成一