Codeforces Round #318 div2

每天一套AK div2 计划开始了。。。

 

A. Bear and Elections
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland.

There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would getai votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate.

Victory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn't have many candies and wonders - how many citizens does he have to bribe?

Input

The first line contains single integer n (2 ≤ n ≤ 100) - number of candidates.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1000) - number of votes for each candidate. Limak is candidate number 1.

Note that after bribing number of votes for some candidate might be zero or might be greater than 1000.

Output

Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate.

Sample test(s)
input
5
5 1 11 2 8
output
4
input
4
1 8 8 8
output
6
input
2
7 6
output
0
Note

In the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9, 1, 7, 2, 8 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate to get situation 9, 0, 8, 2, 8.

In the second sample Limak will steal 2 votes from each candidate. Situation will be 7, 6, 6, 6.

In the third sample Limak is a winner without bribing any citizen.

 

 

题目大意:

  水题一发,就是说,给你n个数字,然后问操作最少的次数使得第一个数字是这n个数字中最大的,操作的细节就是,每次从n-1个数字中找到一个数字,然后对其进行-k,然后a[0]+k操作。

n<=100,直接暴力。

解题思路:

  首先对于a[1]~a[n]进行排序,然后,比较a[0]和a[n-1]的大小,如果a[n-1]比a[0]大,那么就对a[0]++,a[n-1]--,在对这n-1个数字进行排序,时间复杂度最大为O(1000nlogn)

代码:

# include<cstdio>
# include<iostream>
# include<algorithm>

using namespace std;

# define MAX 123

int a[MAX];

int main(void)
{
    int n; cin>>n;
    for ( int i = 0;i < n;i++ )
        cin>>a[i];
    sort(a+1,a+n);
    int tmp = a[0];
    while ( a[0] <= a[n-1] )
    {
        a[0]++;
        a[n-1]--;
        sort(a+1,a+n);
    }
    cout<<a[0]-tmp<<endl;


    return 0;
}

  

 

B. Bear and Three Musketeers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Do you know a story about the three musketeers? Anyway, you will learn about its origins now.

Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys.

There are n warriors. Richelimakieu wants to choose three of them to become musketeers but it's not that easy. The most important condition is that musketeers must know each other to cooperate efficiently. And they shouldn't be too well known because they could be betrayed by old friends. For each musketeer his recognition is the number of warriors he knows, excluding other two musketeers.

Help Richelimakieu! Find if it is possible to choose three musketeers knowing each other, and what is minimum possible sum of their recognitions.

Input

The first line contains two space-separated integers, n and m (3 ≤ n ≤ 4000, 0 ≤ m ≤ 4000) — respectively number of warriors and number of pairs of warriors knowing each other.

i-th of the following m lines contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ nai ≠ bi). Warriors ai and bi know each other. Each pair of warriors will be listed at most once.

Output

If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print "-1" (without the quotes).

Sample test(s)
input
5 6
1 2
1 3
2 3
2 4
3 4
4 5
output
2
input
7 4
2 1
3 6
5 1
1 7
output
-1
Note

In the first sample Richelimakieu should choose a triple 1, 2, 3. The first musketeer doesn't know anyone except other two musketeers so his recognition is 0. The second musketeer has recognition 1 because he knows warrior number 4. The third musketeer also has recognition 1 because he knows warrior 4. Sum of recognitions is 0 + 1 + 1 = 2.

The other possible triple is 2, 3, 4 but it has greater sum of recognitions, equal to 1 + 1 + 1 = 3.

In the second sample there is no triple of warriors knowing each other.

 题目大意:

  就是说,给你n个人和m个关系,(a,b)表示a和b认识,现在这个人想雇佣3个杀手,他所要达成的目的是,怎么样的操作才能使得雇佣的三个杀手认识的人的总和最少。

解题思路:

  一开始一看就是并查集的题目,然后上来就想到的是,首先判断是否会存在环,m种关系都确定结束后,已经没有环出现,那么就输出-1.如果其中有环存在,那么去计算

每个节点的reg,这些reg的总和取最小就是答案,大概写了一下,到有很多环的时候,怎么样判断每个节点的reg就抓瞎了,因为n<=4000,很可能会t。

  于是,选取另外一种方法,就是首先我们用一个n*n的矩阵来记录他们之间的关系。当然这个矩阵是对称的,也就是说,如果a认识b,那么b也就认识a。然后,用num数组记录每个人在关系中出现的次数

接下来枚举第j个人,看这个人和其他的人的关系,有点类似与floyd算法,就是说已经确定了x和y有关系,看看有没有第三个k使得,x和k有关系,k和y还有关系,并且他们的reg最小。

最后不要忘记把reg-6.因为这个形成环的人每次都被算了2遍,但是题目中,对于他们自身的reg并不需要计算。

代码:

# include<cstdio>
# include<iostream>

using namespace std;

# define MAX 4444
# define inf 99999999

int dij[MAX][MAX];
int x[MAX],y[MAX];
int num[MAX];


int main(void)
{
    int n,m; scanf("%d%d",&n,&m);
    for ( int i = 0;i < m;i++ )
    {
        scanf("%d%d",&x[i],&y[i]);
        dij[x[i]][y[i]] = dij[y[i]][x[i]] = 1;
        num[x[i]]++;
        num[y[i]]++;
    }
    int flag = 0;
    int ans = inf;
    for ( int i = 0;i < m;i++ )
    {
        for ( int j = 1;j <= n;j++ )
        {
            if ( dij[x[i]][j]&&dij[j][y[i]] )
            {
                if ( ans > num[x[i]]+num[j]+num[y[i]] )
                {
                    ans = num[x[i]]+num[j]+num[y[i]];
                    flag = 1;
                }
            }
        }
    }
    if ( flag==0 )
    {
        puts("-1");
        return 0;
    }
    else
        printf("%d\n",ans-6);

    return 0;
}
C. Bear and Poker
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars.

Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot?

Input

First line of input contains an integer n (2 ≤ n ≤ 105), the number of players.

The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109) — the bids of players.

Output

Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise.

Sample test(s)
input
4
75 150 75 50
output
Yes
input
3
100 150 250
output
No
Note

In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.

It can be shown that in the second sample test there is no way to make all bids equal.

 

题目大意:

  这个题目是说,现在有n个数字,现在想要把每个数字变为原来的3倍或者2倍,任意多次,使得最后的达到的结果是,这n个数字都相同。

解题思路:

  这道题目实际就是说,我现在手上有n个数字,我只要看看他能不能被2整除,如果能的话,我就一直把它除到底。然后,再看这个数字能不能

被3整除,如果能的话,我就一直把它除到底。最后将对于每个数字处理后的余数放进一个set里面,如果这个set里面最终只有一个元素的话,那么

我们就说这n个数字是可以通过变2倍和变3倍直到最终都相等的答案的。

代码:

# include<cstdio>
# include<iostream>
# include<set>

using namespace std;

set<int>s;

int main(void)
{
    int n; scanf("%d",&n);
    for ( int i = 0;i < n;i++ )
    {
        int tmp; scanf("%d",&tmp);
        while ( tmp%3==0 )
            tmp/=3;
        while ( tmp%2==0 )
            tmp/=2;
        s.insert(tmp);
    }
    if (s.size()==1)
        puts("Yes");
    else
        puts("No");

    return 0;
}

                                                                                                                                                                                                                                                                   

 

 

 

 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

 

 

posted @ 2015-09-03 12:11  BYYB_0506  阅读(220)  评论(0编辑  收藏  举报