K - Kindergarten Election

从max(1, num[1])开始枚举,一直到n-1,每一个枚举的数的意思是:这个票数可以让1号当选唯一的leader,所以在每此枚举时,要从2号到n号,遍历一遍,如果num[i] >= num[1]则就要在支持i号的人当中选出最小代价的num[i] - num[1] + 1位,用糖果收买他,并使nownum ++(每此枚举的初始值是num[1]),注意记录代价,一个遍历完以后,如果nownum==当前枚举的数时,就把这次枚举的总代价和Min相比,取较小的值,,如果nownum > num[1].那么这种情况是不合法的,不符合我们前面枚举的意图,如果nownum < num[1],那么就从剩下的没有被收买的人当中,在收买num[1] - nownum 个人,记录下代价和Min比较,取较小值,思路就是这样
#include <bits/stdc++.h>

using namespace std;

const int inf = 0X3f3f3f3f;
int vote[110],num[110];
int val[110];
bool vis[110];
bool tmp(int x, int y)
{
    return val[x] < val[y];
}
int main()
{
    int sum;
    scanf("%d", &sum);
    while(sum --){
       int n;
       memset(num, 0, sizeof(num));
       scanf("%d", &n);
       for(int i = 2; i <= n; i ++){
            scanf("%d", &vote[i]);
            num[vote[i]] ++;
       }
       for(int i = 2; i <= n; i ++){
            scanf("%d", &val[i]);
       }
       int Min = inf;
       int cost;
       int nownum;
       for(int i = max(1, num[1]); i < n; i ++){
            memset(vis, false, sizeof(vis));
            cost = 0;
            nownum = num[1];
            for(int j = 2; j <= n; j ++){
               if(num[j] >= i){
                    int shu[110];
                    int cnt = 0;
                    int len = num[j] - i + 1;
                    for(int z = 2; z <= n; z ++){
                        if(vote[z] == j){
                            shu[cnt ++] = z;
                        }
                    }
                    sort(shu, shu + cnt, tmp);
                    for(int z = 0; z < len; z ++){
                        //cout << shu[z] << endl;
                        cost += val[shu[z]];
                        vis[shu[z]] = true;
                        nownum ++;

                    }
               }
            }
            if(nownum > i) continue;
            int len = i - nownum;
            //cout << len << endl;
            int shu[110];
            int cnt = 0;
            for(int j = 2; j <= n; j ++){
                if(!vis[j] && vote[j] != 1){
                    shu[cnt ++] = j;
                }
            }
            sort(shu, shu + cnt, tmp);
            for(int j = 0; j < len; j ++){
                cost += val[shu[j]];
               // cout << shu[j] << endl;
            }
            Min = min(cost, Min);
       }
       cout << Min <<endl;
    }
    return 0;
}

Kindergarten Election

04-07

At the beginning of the semester in kindergarten, the n little kids (indexed from 1 to n, for convenience) in class need to elect their new leader.nnThe ith kid will vote for his best friend fi (where 1 ≤ fi ≤ n, and it's too shame to vote for yourself, so fi ≠ i). And the kid who gets the most votes will be the leader. If more than one kids who get the largest number of votes, there will be multiple leaders in the new semester.nnLittle Sheldon (the kid with index 1) is extremely vain, and he would like to be the ONLY leader. (That means the number of votes he gets should strictly larger than any other.) Soon Sheldon found that if he give ci candies to the ith kid, the ith kid would regard Sheldon as the new best friend, and of course vote for Sheldon.nnEvery kid including Sheldon loves candies. As an evil programmer, please help the evil Sheldon become the ONLY leader with minimum cost of candies. By the way, Sheldon should vote for any one he wants EXCEPT himself.nnInputnnThere are multiple test cases. The first line of input contains an integer T (T ≤ 100) indicating the number of test cases. Then T test cases follow.nnThe first line of each case contains one integer: n (3 ≤ n ≤ 100) -- the number of kids in class.nnThe second line contains n-1 integers: fi (1 ≤ fi ≤ n, fi ≠ i, and 2 ≤ i ≤ n) -- represents that the best friend of ith kid is indexed with fi.nnThe third line contains n-1 integers: ci (1 ≤ ci ≤ 1000, and 2 ≤ i ≤ n) -- represents that if Sheldon gave ci candies to the ith kid, the ith kid would vote Sheldon, instead of their old best friend fi, as the new semester leader.nnOutputnnFor each test case, print the minimal cost of candies to help Sheldon become the ONLY leader.nnSample Inputnn2n4n1 1 2n1 10 100n3n3 2n1 10nSample Outputnn0n11

Election

08-05

Canada has a multi-party system of government. Each candidate is generally associated with a party, and the party whose candidates win the most ridings generally forms the government. Some candidates run as independents, meaning they are not associated with any party. Your job is to count the votes for a particular riding and to determine the party with which the winning candidate is associated.nnnInputnnThe first line of input contains a positive integer n satisfying 2 <= n <= 20, the number of candidates in the riding. n pairs of lines follow: the first line in each pair is the name of the candidate, up to 80 characters; the second line is the name of the party, up to 80 characters, or the word "independent" if the candidate has no party. No candidate name is repeated and no party name is repeated in the input. No lines contain leading or trailing blanks. nnThe next line contains a positive integer m <= 10000, and is followed by m lines each indicating the name of a candidate for which a ballot is cast. Any names not in the list of candidates should be ignored.nnInput contains multiple tests. Process to the end of file.nnnOutputnnOutput consists of a single line containing one of: nnThe name of the party with whom the winning candidate is associated, if there is a winning candidate and that candidate is associated with a party. nnThe word "independent" if there is a winning candidate and that candidate is not associated with a party. nnThe word "tie" if there is no winner; that is, if no candidate receives more votes than every other candidate.nnnSample Inputnn3nMarilyn MansonnRhinocerosnJane DoenFamily CoalitionnJohn Smithnindependentn6nJohn SmithnMarilyn MansonnMarilyn MansonnJane DoenJohn SmithnMarilyn MansonnnnSample OutputnnRhinoceros

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭