Codeforces Round #263 (Div. 2) A B C

题目链接

A. Appleman and Easy Task

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?

Given a n × n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the board are adjacent if they share a side.

Input

The first line contains an integer n (1 ≤ n ≤ 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either 'x' or 'o') without spaces.

Output

Print "YES" or "NO" (without the quotes) depending on the answer to the problem.

Sample test(s)
input
3
xxo
xox
oxx
output
YES
input
4
xxxo
xoxo
oxox
xxxx
output
NO


题意 :n*n的格子,问是否每个格子都与偶数个o相邻

思路 :暴力。。。最讨厌这种怎么看都对的数据了。。。。

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <iostream>

 4 #include <stdlib.h>

 5 #include <algorithm>

 6 

 7 using namespace std ;

 8 

 9 char sh[110][110] ;

10 

11 int main()

12 {

13     int n ;

14     scanf("%d",&n) ;

15     for(int i = 0; i < n ; i++)

16         scanf("%s",sh[i]) ;

17         bool flag = false ;

18     for(int i = 0 ; i < n ; i++)

19     {

20         for(int j = 0 ; j < n ; j++)

21         {

22             int cnt = 0 ;

23             if(i > 0 && sh[i-1][j] == 'o') cnt ++ ;

24             if(i < n-1 && sh[i+1][j] == 'o') cnt++ ;

25             if(j > 0 && sh[i][j-1] == 'o') cnt ++ ;

26             if(j < n-1 && sh[i][j+1] == 'o') cnt ++;

27             if(cnt % 2)

28             {

29                 flag = true ;break ;

30             }

31         }

32         if(flag) break ;

33     }

34     if(flag) puts("NO") ;

35     else puts("YES") ;

36     return 0 ;

37 }
View Code

B. Appleman and Card Game

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.

Given the description of Appleman's cards. What is the maximum number of coins Toastman can get?

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.

Output

Print a single integer – the answer to the problem.

Sample test(s)
input
15 10
DZFDFZDFDDDDDDF
output
82
input
6 4
YJSNPI
output
4
Note

In the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin.

 

题意 :一共n个大写字母,从中挑出k个,这k个中的对于每个字母来说,有x个字母都为该字母,那最后的值就加上x,问最后怎么选这k个字母使得最后的值最大。

思路 :贪心,哈希一下,排序,然后从最多的开始找,要注意,这个题卡long long 。。。。。因为中间结果可能会超什么的。。。。要注意。。。。

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <iostream>

 4 #include <stdlib.h>

 5 #include <algorithm>

 6 

 7 using namespace std ;

 8 

 9 char sh[100010] ;

10 long long hashh[100] ;

11 

12 int main()

13 {

14     long long n , k ;

15   //  freopen("1234.txt","r",stdin) ;

16    // freopen("1234.txt","w",stdout) ;

17     while(~scanf("%I64d %I64d",&n,&k))

18     {

19         scanf("%s",sh) ;

20         memset(hashh,0,sizeof(hashh)) ;

21         for(int i = 0;  i < n ; i++)

22         {

23             hashh[sh[i]-'A'] ++ ;

24         }

25         sort(hashh,hashh+26) ;

26         long long sum = 0 ;

27         for(long long i = k ,j = 25; i > 0 ; )

28         {

29             if(hashh[j] <= i)

30             {

31                 sum += hashh[j]*hashh[j] ;

32                 i -= hashh[j] ;

33             }

34             else

35             {

36                 sum += i*i ;

37                 i = 0 ;

38             }

39            j-- ;

40         }

41         printf("%I64d\n",sum) ;

42     }

43     return 0 ;

44 }
View Code

C. Appleman and Toastman

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:

  • Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
  • Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.

After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?

Input

The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.

Output

Print a single integer — the largest possible score.

Sample test(s)
input
3
3 1 5
output
26
input
1
10
output
10
Note

Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.

 

题意 :给T一个数组,然后他把所有的值加起来加到最后的值上,然后把这个数组给A,如果A收到的数组只有一个数,那就把这个值扔掉。否则的话A就把这个数组分成两个再送给T,然后T再把和加到最后的值上。。。。循环往复,问最后得到的最大值是多少。

思路 : 其实就是排一下序,然后每次把最小的分出去,最后统计一下每个值被加过的次数,发现最后一个是n次,倒数第二个也是n次,第一个是2次,第二个是3次,第三个是4次,第四个是5次。。。。。

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <iostream>

 4 #include <algorithm>

 5 #define LL long long

 6 

 7 using namespace std ;

 8 

 9 LL n ;

10 LL a[301010] ;

11 

12 int main()

13 {

14     while(~scanf("%I64d",&n))

15     {

16         for(int i = 1 ; i <= n ; i++)

17         {

18             scanf("%I64d",&a[i]) ;

19         }

20         LL ans = 0 ;

21         sort(a+1,a+n+1) ;

22         for(int i = 1 ; i < n ; i++)

23         {

24             ans += a[i] * (i+1) ;

25 //            printf("sum = %I64d\n",a[i]*(i+1)) ;

26 //            printf("ans = %I64d\n",ans) ;

27         }

28         ans += n * a[n] ;

29         printf("%I64d\n",ans) ;

30     }

31     return 0 ;

32 }
View Code

 

你可能感兴趣的:(codeforces)