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.
A 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.
The first line of the input contains integers nn (1≤n≤1051≤n≤105) and kk (1≤k≤261≤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.
Print the only integer — the length of the longest good subsequence of string ss.
9 3
ACAABCCAB
6
9 4
ABCABCABC
0
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 }
Find out if it is possible to partition the first nn positive integers into two non-empty disjoint sets S1S1 and S2S2 such that:
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.
The only line of the input contains a single integer nn (1≤n≤450001≤n≤45000)
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.
1
No
3
Yes
1 2
2 1 3
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 }
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.
The first line of input contains an integer nn (1≤n≤1000001≤n≤100000) — the sizes of the list.
The second line contains nn integers aiai (1≤ai≤1061≤ai≤106), describing the list of the player A, who starts the game.
The third line contains nn integers bibi (1≤bi≤1061≤bi≤106), describing the list of the player B.
Output the difference between A's score and B's score (A−BA−B) if both of them are playing optimally.
2
1 4
5 1
0
3
100 100 100
100 100 100
0
2
2 1
5 6
-3
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 }