hdu 5090 Game with Pearls 同余类

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090

题目大意是有n个数 给一个k

每次你只能给其中一个数加上“k的非负整数倍的值”(包括0)

问最终能否让这堆数变成从1到n各有一个

简单排序完贪心是不对的

正确的思想是同余类

因为实质上每次只能加k,所以模k同余的可归为一类

然后分别对每一个同余类是否有可能合法

不合法的情形有两种 a.该同余类内的数不够多或太多 b.该同余类内的对应位置的数不够小

如果按这种想法走还需特判一下模k得0的情况

总是可能是我写搓了 感觉本来挺简单的一个题怎么遍地是坑

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

const int maxn = 110;

int T;
int a[maxn];
int b[maxn];
int c[maxn][maxn];
int cnt[maxn];
int main()
{
    //freopen("in.txt", "r", stdin);

    scanf("%d", &T);
    while(T--)
    {
        memset(cnt, 0, sizeof(cnt));

        int n, k, m;
        scanf("%d%d", &n, &k);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);

        for(int i = 1; i <= n; i++)
        {
            b[i] = a[i] % k;
            if(b[i] == 0)
                b[i] += k;
        }

        for(int i = 1; i <= n; i++)
        {
            int loc = b[i];
            c[loc][cnt[loc]++] = a[i];
        }

        m = n % k;

        bool flag = true;

        for(int i = 1; i <= k && flag; i++)
        {
            if(i <= m)
            {
                if(cnt[i] != n/k + 1)
                    flag = false;
            }
            else if(cnt[i] != n/k)
                flag = false;
        }

        for(int i = 1; i <= k && flag; i++)
            sort(c[i], c[i] + cnt[i]);

        for(int i = 1; i <= k && flag; i++)
        {
            int ub = i;
            for(int j = 0; j < cnt[i] && flag; j++)
            {
                if(c[i][j] > ub)
                    flag = false;
                ub += k;
            }
        }

        if(flag)
            printf("Jerry\n");
        else
            printf("Tom\n");
    }

    return 0;
}
时间: 2024-10-12 06:51:56

hdu 5090 Game with Pearls 同余类的相关文章

HDU 5090 Game with Pearls(二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 Problem Description Tom and Jerry are playing a game with tubes and pearls. The rule of the game is: 1) Tom and Jerry come up together with a number K. 2) Tom provides N tubes. Within each tube, the

[HDU 5090] Game with Pearls (贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 题目大意:给你n个数,问你给若干个数增加c*k(c>=0)能否组成1,2,3,4,5,...,n? 今天下午作比赛的时候,我先用了个dfs,看这个a[i]匹配的是1到n的哪个数. 后来TLE到死... 仔细想想,首先,如果这个数是小的数的话,那么它可以匹配很多种,因此如果先将它匹配掉了会浪费,因为它“能力”大. 因此就可以排个序,从大到小进行匹配. 我也不知道怎么用数学语言去证明... 1 #

hdu 5090 Game with Pearls(最大匹配)

Game with Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 196    Accepted Submission(s): 120 Problem Description Tom and Jerry are playing a game with tubes and pearls. The rule of the g

HDU 5090 Game with Pearls

直接贪心就可以了,接收的时候把0都加上k,每次从最小的数开始找,如果有多个相同的,那么保留一个,其他的都加上k #include<stdio.h> #include<algorithm> using namespace std; int a[105]; int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif int T,n,k; scanf("%d&q

hdu 5090 Game with Pearls (额,, 想法题吧 / 二分图最大匹配也可做)

题意: 给你N个数,a1,,,,an.代表第i个管子里有ai个珍珠. 规定只能往每根管里增加k的倍数个珍珠. 如果存在一套操作,操作完毕后可以得到1~N的一个排列,则Jerry赢,否则Tom赢. 问谁赢. 思路: 将a1...an从小到大排序,可知道每根管里的数只能增不能减.将最后的1...N中的每个数一定是由小于等于它的数加上若干个K得到来的. 额..直接看代码吧 代码: int a[1005]; int m,n,k; int main(){ //freopen("test.in",

hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8984    Accepted Submission(s): 4594 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球

贪心 HDOJ 5090 Game with Pearls

题目传送门 1 /* 2 题意:给n, k,然后允许给某一个数加上k的正整数倍,当然可以不加, 3 问你是否可以把这n个数变成1,2,3,...,n, 可以就输出Jerry, 否则输出Tom. 4 贪心:保存可能变成的值的方案数,当一个符合,其他所有可能方案减1 5 最大匹配 详细解释:http://blog.csdn.net/u012596172/article/details/40784773?utm_source=tuicool 6 */ 7 #include <cstdio> 8 #i

「POJ3539」Elevator - 同余类最短路

->戳我进原题 Elevator Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 1572 Accepted: 424 Case Time Limit: 2000MS Description Edward works as an engineer for Non-trivial Elevators: Engineering, Research and Construction (NEERC). His new task is

HDU 6071 Lazy Running (同余最短路 dij)

Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1384    Accepted Submission(s): 597 Problem Description In HDU, you have to run along the campus for 24 times, or you will fail in