Codeforces Round #508(ABCD)

A. Equality
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string ss of length nn, which consists only of the first kk letters of the Latin alphabet. All letters in string ss are uppercase.

subsequence of string ss is a string that can be derived from ss by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.

A subsequence of ss called good if the number of occurences of each of the first kk letters of the alphabet is the same.

Find the length of the longest good subsequence of ss.

Input

The first line of the input contains integers nn (1n1051≤n≤105) and kk (1k261≤k≤26).

The second line of the input contains the string ss of length nn. String ss only contains uppercase letters from 'A' to the kk-th letter of Latin alphabet.

Output

Print the only integer — the length of the longest good subsequence of string ss.

Examples
input
Copy
9 3
ACAABCCAB
output
Copy
6
input
Copy
9 4
ABCABCABC
output
Copy
0
Note

In the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.

In the second example, none of the subsequences can have 'D', hence the answer is 00.

标记字母出现的次数

 1 #include <iostream>
 2 using namespace std;
 3 int an[26];
 4 int main(){
 5     int n,m;
 6     cin>>n>>m;
 7     string s;
 8     cin>>s;
 9     for(int i = 0; i < s.length(); ++i){
10         an[s[i]-'A']++;
11     }
12     int ans = 100005;
13     for(int i = 0; i < m; ++i){
14         ans = min(ans,an[i]);
15     }
16     if(ans == 100005)
17         ans = 0;
18     cout<<ans*m<<endl;
19     return 0;
20 }
B. Non-Coprime Partition
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Find out if it is possible to partition the first nn positive integers into two non-empty disjoint sets S1S1 and S2S2 such that:

gcd(sum(S1),sum(S2))>1gcd(sum(S1),sum(S2))>1

Here sum(S)sum(S) denotes the sum of all elements present in set SS and gcdgcd means thegreatest common divisor.

Every integer number from 11 to nn should be present in exactly one of S1S1 or S2S2.

Input

The only line of the input contains a single integer nn (1n450001≤n≤45000)

Output

If such partition doesn't exist, print "No" (quotes for clarity).

Otherwise, print "Yes" (quotes for clarity), followed by two lines, describing S1S1 and S2S2 respectively.

Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty.

If there are multiple possible partitions — print any of them.

Examples
input
Copy
1
output
Copy
No
input
Copy
3
output
Copy
Yes
1 2
2 1 3
Note

In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is "No".

In the second example, the sums of the sets are 22 and 44 respectively. The gcd(2,4)=2>1gcd(2,4)=2>1, hence that is one of the possible answers.

判断是否为素数。

然后按二进制插开输出。

 1 #include <bits/stdc++.h>
 2 #define N 45005
 3 using namespace std;
 4 int cnt = 0;
 5 
 6 bool isprime(int x){
 7     bool flag = true;
 8     for(int i=2;i<=sqrt(x);i++){
 9         if(x%i==0){
10             cnt = i;
11             flag = false;
12             break;
13         }
14     }
15     return flag;
16 }
17 int a[N];
18 int vis[N];
19 int main(){
20     int n;
21     cin>>n;
22     int ans = n*(n+1)/2;
23     bool prime = isprime(ans);
24     if(prime){
25         cout<<"No"<<endl;
26     }else{
27         cout<<"Yes"<<endl;
28         int ans = 0;
29         int index = 1;
30         while(cnt){
31             if(cnt&1)
32                 a[ans++] = index,vis[index] = 1;
33             index <<= 1;
34             cnt >>= 1; 
35         }
36         cout<<ans<<" ";
37         for(int i=0;i<ans;i++)
38             cout<<a[i]<<" ";
39         cout<<endl;
40         cout<<n-ans<<" ";
41         for(int i=1;i<=n;i++)
42             if(vis[i]==0)
43                 cout<<i<<" ";
44         cout<<endl;
45     }
46     return 0;
47 }
C. Gambling
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two players A and B have a list of nn integers each. They both want to maximize the subtraction between their score and their opponent's score.

In one turn, a player can either add to his score any element from his list (assuming his list is not empty), the element is removed from the list afterward. Or remove an element from his opponent's list (assuming his opponent's list is not empty).

Note, that in case there are equal elements in the list only one of them will be affected in the operations above. For example, if there are elements {1,2,2,3}{1,2,2,3} in a list and you decided to choose 22 for the next turn, only a single instance of 22 will be deleted (and added to the score, if necessary).

The player A starts the game and the game stops when both lists are empty. Find the difference between A's score and B's score at the end of the game, if both of the players are playing optimally.

Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. In this problem, it means that each player, each time makes a move, which maximizes the final difference between his score and his opponent's score, knowing that the opponent is doing the same.

Input

The first line of input contains an integer nn (1n1000001≤n≤100000) — the sizes of the list.

The second line contains nn integers aiai (1ai1061≤ai≤106), describing the list of the player A, who starts the game.

The third line contains nn integers bibi (1bi1061≤bi≤106), describing the list of the player B.

Output

Output the difference between A's score and B's score (ABA−B) if both of them are playing optimally.

Examples
input
Copy
2
1 4
5 1
output
Copy
0
input
Copy
3
100 100 100
100 100 100
output
Copy
0
input
Copy
2
2 1
5 6
output
Copy
-3
Note

In the first example, the game could have gone as follows:

  • A removes 55 from B's list.
  • B removes 44 from A's list.
  • A takes his 11.
  • B takes his 11.

Hence, A's score is 11, B's score is 11 and difference is 00.

There is also another optimal way of playing:

  • A removes 55 from B's list.
  • B removes 44 from A's list.
  • A removes 11 from B's list.
  • B removes 11 from A's list.

The difference in the scores is still 00.

In the second example, irrespective of the moves the players make, they will end up with the same number of numbers added to their score, so the difference will be 00.

当自己的最大值大于别人的最大值时,就加自己的为分数,否则就删掉别人的。

 1 #include <bits/stdc++.h>
 2 #define ll long long int
 3 using namespace std;
 4 
 5 priority_queue<ll, vector<ll>, less<ll> > qa,qb;
 6 ll n,x;
 7 int main(){
 8     cin>>n;
 9     for (int i = 0; i < n; ++i)
10         cin>>x,qa.push(x);
11     
12     for (int i = 0; i < n; ++i)
13         cin>>x,qb.push(x);
14     ll aa = 0,bb = 0;
15     while(!qa.empty()||!qb.empty()){
16         if(qa.empty()){
17             qb.pop();
18         }else{
19             if(qb.empty() || qa.top() > qb.top()){
20                 aa += qa.top(), qa.pop();
21             }else{
22                 qb.pop();
23             }
24         }
25 
26         if(qb.empty()){
27             qa.pop();
28         }else{
29             if(qa.empty() || qb.top() > qa.top()){
30                 bb += qb.top(), qb.pop();
31             }else{
32                 qa.pop();
33             }
34         }
35     }
36     cout<<aa-bb<<endl;
37     return 0;
38 }
D. Slime
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are nn slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it.

Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists).

When a slime with a value xx eats a slime with a value yy, the eaten slime disappears, and the value of the remaining slime changes to xyx−y.

The slimes will eat each other until there is only one slime left.

Find the maximum possible value of the last slime.

Input

The first line of the input contains an integer nn (1n5000001≤n≤500000) denoting the number of slimes.

The next line contains nn integers aiai (109ai109−109≤ai≤109), where aiai is the value of ii-th slime.

Output

Print an only integer — the maximum possible value of the last slime.

Examples
input
Copy
4
2 1 2 1
output
Copy
4
input
Copy
5
0 -1 -1 -1 -1
output
Copy
4
Note

In the first example, a possible way of getting the last slime with value 44 is:

  • Second slime eats the third slime, the row now contains slimes 2,1,12,−1,1
  • Second slime eats the third slime, the row now contains slimes 2,22,−2
  • First slime eats the second slime, the row now contains 44

In the second example, the first slime can keep eating slimes to its right to end up with a value of 44.

当非负数和负数同事存在时,则只需要绝对值的累加和。

否则的话就是制造一个非正数。然后取最大值。

只需要遍历一次即可。

 1 #include <bits/stdc++.h>
 2 #define N 500000
 3 #define ll long long int
 4 using namespace std;
 5 
 6 int n;
 7 ll a[N];
 8 int main(){
 9     scanf("%d",&n);
10     ll ans = 0;
11     bool prime = true, flag = true;
12     for(int i = 0; i < n; ++i){
13         scanf("%lld", &a[i]);
14         ans += abs(a[i]);
15         if(a[i]<0)
16             prime = false;
17         else
18             flag = false;
19     }
20     if(n==1)
21         cout<<a[0]<<endl;
22     else{
23         if(!prime && !flag){
24             cout<<ans<<endl;
25         }else{
26             ll cnt = 0;
27             for(int i = 1; i < n; ++i){
28                 ll an = ans - abs(a[i-1]) - abs(a[i]) + abs(a[i-1] - a[i]);
29                 cnt = max(cnt,an);
30             }
31             cout<<cnt<<endl;
32         }
33     }
34     return 0;
35 }
原文地址:https://www.cnblogs.com/zllwxm123/p/9642774.html