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 s are uppercase.

subsequence of string 

A subsequence of k letters of the alphabet is the same.

Find the length of the longest good subsequence of s.

Input

The first line of the input contains integers 1≤k≤26).

The second line of the input contains the string k-th letter of Latin alphabet.

Output

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

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 0.

 

标记字母出现的次数

 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 S2 such that:

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

Here greatest common divisor.

Every integer number from S2.

Input

The only line of the input contains a single integer 1≤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 S2 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 gcd(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 n 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 2 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 1≤n≤100000) — the sizes of the list.

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

The third line contains 1≤bi≤106), describing the list of the player B.

Output

Output the difference between A's score and B's score (A−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 5 from B's list.
  • B removes 4 from A's list.
  • A takes his 1.
  • B takes his 1.

Hence, A's score is 0.

There is also another optimal way of playing:

  • A removes 5 from B's list.
  • B removes 4 from A's list.
  • A removes 1 from B's list.
  • B removes 1 from A's list.

The difference in the scores is still 0.

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 0.

 

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

 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 n 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 x−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 1≤n≤500000) denoting the number of slimes.

The next line contains i-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 4 is:

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

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

 

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

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

只需要遍历一次即可。

 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 }

 

 

 

相关文章:

  • 2021-12-11
  • 2021-12-08
  • 2021-10-01
  • 2021-09-25
  • 2022-01-24
  • 2021-12-18
  • 2021-06-25
  • 2021-08-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-04-07
  • 2022-02-11
  • 2022-02-11
  • 2021-12-07
  • 2021-10-27
相关资源
相似解决方案