这周打了一场cf,ABC三道题还没有全ac,B没过,没有及时止损,依然掉分中,
课上讲得二分法之前也训练过,感觉还算可以,
这周的劳动节真的是劳动,累死。。。。
C. Phoenix and Distribution
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Phoenix has a string s consisting of lowercase Latin letters. He wants to distribute all the letters of his string into k non-empty strings a_1, a_2, \dots, a_ksuch that every letter of s goes to exactly one of the strings a_i. The stringsa_i do not need to be substrings ofs. Phoenix can distribute letters of s and rearrange the letters within each string a_i however he wants.
For example, if s = baba and k=2, Phoenix may distribute the letters of his string in many ways, such as:
ba and ba
a and abb
ab and ab
aa and bb
But these ways are invalid:
baa and ba
b and ba
baba and empty string (a_ishould be non-empty)
Phoenix wants to distribute the letters of his string s into k strings a_1, a_2, \dots, a_k to minimize the lexicographically maximum string among them, i. e. minimize max(a_1, a_2, \dots, a_k). Help him find the optimal distribution and print the minimal possible value of max(a_1, a_2, \dots, a_k).
String x is lexicographically less than string y if either x is a prefix of yand x \ne y, or there exists an index i (1 \le i \le min(|x|, |y|)) such thatx_i < y_i and for every j(1 \le j < i)x_j = y_j. Here |x| denotes the length of the string x.
Input
The input consists of multiple test cases. The first line contains an integer t (1 \le t \le 1000) — the number of test cases. Each test case consists of two lines.
The first line of each test case consists of two integers n and k (1 \le k \le n \le 10^5 — the length of string s and the number of non-empty strings, into which Phoenix wants to distribute letters of s, respectively.
The second line of each test case contains a string s of length n consisting only of lowercase Latin letters.
It is guaranteed that the sum of n over all test cases is \le 10^5.
Output
Print t answers — one per test case. The i-th answer should be the minimal possible value of max(a_1, a_2, \dots, a_k) in the i-th test case.
Example
input
6
4 2
baba
5 2
baacb
5 3
baacb
5 3
aaaaa
6 4
aaxxzz
7 1
phoenix
output
ab
abbc
b
aa
x
ehinopx
Note
In the first test case, one optimal solution is to distribute baba into ab and ab.
In the second test case, one optimal solution is to distribute baacb into abbc and a.
In the third test case, one optimal solution is to distribute baacb into ac, ab, and b.
In the fourth test case, one optimal solution is to distribute aaaaa into aa, aa, and a.
In the fifth test case, one optimal solution is to distribute aaxxzz into az, az, x, and x.
In the sixth test case, one optimal solution is to distribute phoenix into ehinopx.
c先排个序 然后分情况考虑一下就行
就是如果前k个有不同的 直接输入一个字母 字母最大的那个
如果都相同 看后面不同的个数 如果大于1 就全分配到首字母后面
#include
#include
#include
using namespace std;
bool cmp(char a,char b)
{
return a<b;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,k,e=1,f=1;
cin>>n>>k;
char a[100005];
for(int i=1; i<=n; i++)
cin>>a[i];
sort(a+1,a+n+1,cmp);
for(int i=2; i<=k; i++)
if(a[i-1]!=a[i])
{
e=0;
break;
}
for(int i=k+2; i<=n; i++)
if(a[i-1]!=a[i])
{
f=0;
break;
}
if(e==0)
{
cout<<a[k]<<endl;
continue;
}
if(e==1)
cout<<a[k];
if(f==0)
for(int i=k+1; i<=n; i++)
cout<<a[i];
if(f==1)
cout<<a[k+1];
cout<<endl;
}
}
A. Phoenix and Balance
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Phoenix has n coins with weights 21,22,…,2n. He knows that n is even.
He wants to split the coins into two piles such that each pile has exactly n2 coins and the difference of weights between the two piles is minimized. Formally, let a denote the sum of weights in the first pile, and b denote the sum of weights in the second pile. Help Phoenix minimize |a−b|, the absolute value of a−b.
Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains an integer n (2≤n≤30; n is even) — the number of coins that Phoenix has.
Output
For each test case, output one integer — the minimum possible difference of weights between the two piles.
Example
input
2
2
4
output
2
6
Note
In the first test case, Phoenix has two coins with weights 2 and 4. No matter how he divides the coins, the difference will be 4−2=2.
In the second test case, Phoenix has four coins of weight 2, 4, 8, and 16. It is optimal for Phoenix to place coins with weights 2 and 16 in one pile, and coins with weights 4 and 8 in another pile. The difference is (2+16)−(4+8)=6.
#include
#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
long long a=0,b=0;
cin>>n;
if(n==2)
{
cout<<2<<endl;
continue;
}
int i=1,j=n;
for(; i<n/2; i++)
a+=pow(2,i);
a+=pow(2,j);
for(; i<n; i++)
b+=pow(2,i);
cout<<a-b<<endl;
}
}
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance before he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Line 1: Three space-separated integers: L, N, and M
Lines 2… N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
通过二分法枚举答案,首先确定left=0,right=河的长度,当两块石头的距离小于mid时,就移走此时这块,并记录移走石头的数目,当大于mid时,更新前一块石头的位置。
#include
#include
#include
using namespace std;
typedef long long ll;
#define M 100001
bool cmp(int a,int b)
{
return a<b;
}
ll a[M];
int main()
{
ll len;
int n,m;
while(cin>>len>>n>>m)
{
a[0]=0;
a[n+1]=len;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a,a+n+2,cmp);
int left=1,right=len;
ll mid;
while(left<=right)
{
int num=0;
mid=(left+right)/2;
int last=0;
for(int i=1;i<=n;i++)
{
if(a[i]-last<mid)
num++;
else
last=a[i];
}
if(num<=m)
left=mid+1;
else
right=mid-1;
}
cout<<right<<endl;
}
return 0;
}