传送门.
Let’s say we would like to give change of d dollars after a puchase of coffee. The only coins that we can use have values a, a+1, …, b. Is it possible to give out the change? We are interested in even harder quesion: for given d and a what is the smallest b such that the change can be given?
Input
The input is two numbers 1≤a≤d≤109.
Output
Print smallest b such that d is a sum of coins from range a,…,b.
Examples
Input
19 60
Output
20
Input
100 914
Output
102
#include
#include
int main()
{
int d,i,a,n,v;
scanf("%d%d",&a,&d);
n=d/a;
v=d%a;
i=ceil(1.0*v/n);
printf("%d\n",a+i);
return 0;
}
传送门.
Vasya has a string s of length n. He decides to make the following modification to the string:
Pick an integer k, (1≤k≤n).
For i from 1 to n−k+1, reverse the substring s[i:i+k−1] of s. For example, if string s is qwer and k=2, below is the series of transformations the string goes through:
qwer (original string)
wqer (after reversing the first substring of length 2)
weqr (after reversing the second substring of length 2)
werq (after reversing the last substring of length 2)
Hence, the resulting string after modifying s with k=2 is werq.
Vasya wants to choose a k such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of k. Among all such k, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.
A string a is lexicographically smaller than a string b if and only if one of the following holds:
a is a prefix of b, but a≠b;
in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
Each test contains multiple test cases.
The first line contains the number of test cases t (1≤t≤5000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤5000) — the length of the string s.
The second line of each test case contains the string s of n lowercase latin letters.
It is guaranteed that the sum of n over all test cases does not exceed 5000.
Output
For each testcase output two lines:
In the first line output the lexicographically smallest string s′ achievable after the above-mentioned modification.
In the second line output the appropriate value of k (1≤k≤n) that you chose for performing the modification. If there are multiple values of k that give the lexicographically smallest string, output the smallest value of k among them.
Example
Input
6
4
abab
6
qwerty
5
aaaaa
6
alaska
9
lfpbavjsm
1
p
Output
abab
1
ertyqw
3
aaaaa
1
aksala
6
avjsmbpfl
5
p
1
Note
In the first testcase of the first sample, the string modification results for the sample abab are as follows :
for k=1 : abab
for k=2 : baba
for k=3 : abab
for k=4 : baba
The lexicographically smallest string achievable through modification is abab for k=1 and 3. Smallest value of k needed to achieve is hence 1.
#include
#include
#include
using namespace std;
string a,b,c,s,x;
int main()
{
int t,n,i,ans;
cin>>t;
while(t--)
{
cin>>n;
cin>>a;
x=a;
ans = 1;
for(i=1; i<=n; i++)
{
b=a.substr(0,i-1);
c=a.substr(i-1,n-i+1);
if((n-i)%2==0)
reverse(b.begin(),b.end());
s=c+b;
if(x>s)
{
x=s;
ans=i;
}
}
cout<<x<<endl;
cout<<ans<<endl;
}
return 0;
}
传送门
Dr. Evil is up to his evil antics again and has traveled back in time to 1975 to enact his evil schemes. The British Intelligence Agency needs someone to travel back in time as well to thwart his convoluted plot. Seeing the opportunity to be part of an adventure that involves riveting mysteries, amorous escapades, and gratuitous violence, you volunteer for the role of 007.
What the British Intelligence Agency failed to tell you was that in this iteration, not only will you be agent 007. You will also be 007 years old and do 007-year-old things. As part of your role, you will receive encrypted messages with a secret marker and you have to decode whether it’s meant for Agent 003, Agent 005, or Agent 007 (you). The marker will be divisible by 3 if it’s meant for Agent 003, divisible by 5 if it’s meant for Agent 005, and 7 if it’s meant for Agent 007.
Since you retained your mental maturity, you want to avoid doing tedious tasks equivalent to homework for 007-year-olds. You want to leave it to a computer to do the job.
The program you write must accept a number m (the secret marker) and output AGENT 003 if it’s divisible by 3, AGENT 005 if it’s divisible by 5, and AGENT 007 if it’s divisible by 7.
Input
The first line of input contains a single integer t, the number of test cases.
Each test case consists of a single line containing a single integer, m.
Constraints
1≤t≤105
1≤m≤1018
Output
For each test case, output several lines. For each agent the message is meant for—AGENT 003, AGENT 005, and/or AGENT 007—output the agent’s name in a single line. If the message is meant for none of you, output NONE. If the message is meant for more than one of you, output each agent in the following order: AGENT 003, AGENT 005, and/or AGENT 007. At the end of the output for each test case, output a single line containing three dashes: —
Example
Input
7
42
420
111
1111
2020
489
123456789012345678
Output
AGENT 003
AGENT 007
—
AGENT 003
AGENT 005
AGENT 007
—
AGENT 003
—
NONE
—
AGENT 005
—
AGENT 003
—
AGENT 003
—
#include
typedef long long ll;
int main()
{
ll t,m,cnt;
scanf("%lld",&m);
while(m--)
{
cnt=0;
scanf("%lld",&t);
if(t%3==0)
{
printf("AGENT 003\n");cnt++;
}
if(t%5==0||t%10==0)
{
printf("AGENT 005\n");cnt++;
}
if(t%7==0)
{
printf("AGENT 007\n");cnt++;
}
if(cnt==0) printf("NONE\n");
printf("---");
if(m) printf("\n");
}
return 0;
}
传送门
Suppose you are performing the following algorithm. There is an array v1,v2,…,vn filled with zeroes at start. The following operation is applied to the array several times — at i-th step (0-indexed) you can:
either choose position pos (1≤pos≤n) and increase vpos by ki;
or not choose any position and skip this step.
You can choose how the algorithm would behave on each step and when to stop it. The question is: can you make array v equal to the given array a (vj=aj for each j) after some step?
Input
The first line contains one integer T (1≤T≤1000) — the number of test cases. Next 2T lines contain test cases — two lines per test case.
The first line of each test case contains two integers n and k (1≤n≤30, 2≤k≤100) — the size of arrays v and a and value k used in the algorithm.
The second line contains n integers a1,a2,…,an (0≤ai≤1016) — the array you’d like to achieve.
Output
For each test case print YES (case insensitive) if you can achieve the array a after some step or NO (case insensitive) otherwise.
Example
Input
5
4 100
0 0 0 0
1 2
1
3 4
1 4 1
3 2
0 1 3
3 9
0 59049 810
Output
YES
YES
NO
NO
YES
Note
In the first test case, you can stop the algorithm before the 0-th step, or don’t choose any position several times and stop the algorithm.
In the second test case, you can add k0 to v1 and stop the algorithm.
In the third test case, you can’t make two 1 in the array v.
In the fifth test case, you can skip 90 and 91, then add 92 and 93 to v3, skip 94 and finally, add 95 to v2.
传送门
n students are taking an exam. The highest possible score at this exam is m. Let ai be the score of the i-th student. You have access to the school database which stores the results of all students.
You can change each student’s score as long as the following conditions are satisfied:
All scores are integers
0≤ai≤m
The average score of the class doesn’t change.
You are student 1 and you would like to maximize your own score.
Find the highest possible score you can assign to yourself such that all conditions are satisfied.
Input
Each test contains multiple test cases.
The first line contains the number of test cases t (1≤t≤200). The description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n≤103, 1≤m≤105) — the number of students and the highest possible score respectively.
The second line of each testcase contains n integers a1,a2,…,an (0≤ai≤m) — scores of the students.
Output
For each testcase, output one integer — the highest possible score you can assign to yourself such that both conditions are satisfied._
Example
Input
2
4 10
1 2 3 4
4 5
1 2 3 4
Output
10
5
Note
In the first case, a=[1,2,3,4], with average of 2.5. You can change array a to [10,0,0,0]. Average remains 2.5, and all conditions are satisfied.
In the second case, 0≤ai≤5. You can change a to [5,1,1,3]. You cannot increase a1 further as it will violate condition 0≤ai≤m.
#include
int main()
{
int i,t,m,n,sum,a[1005];
scanf("%d",&t);
while(t--)
{
sum=0;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
if(sum>m)
printf("%d\n",m);
else printf("%d\n",sum);
}
return 0;
}
传送门
Alicia has a huge garden which is the habitat of many animals that she really cares about. After listening to a podcast about biodiversity, she becomes very concerned about the balance between species in her garden. She wants to know if there is a species that could overtake the others. In order to do so, she decides to carry out a census of all animals in the garden, writing down the species of each of them. Can you help her checking if there is strictly more animals from one species than the animals from all other species together?
Input
The input consists of the following lines:
on the first line: an integer N;
on each of the next N lines: the species of an animal as a string of length at most 20, containing only ASCII alphanumeric characters.
Limits
1≤N≤2×105.
Output
A string that appears a number of times that is greater than the sum of the others, if there is any, or the string “NONE” otherwise.
Examples
Input
3
frog
fish
frog
Output
frog
Input
4
cat
mouse
mouse
cat
Output
NONE
#include
#include
using namespace std;
map<string,int> a;
string s,ans;
int main()
{
int n,max=0;
cin>>n;
for(int i=0; i<n; i++)
{
cin>>s;
a[s]+=1;
if(max<a[s])
{
max=a[s];
ans=s;
}
}
if(max>n-max)
cout<<ans;
else
cout<<"NONE";
return 0;
}
传送门
Vova promised himself that he would never play computer games… But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants c i gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven’t understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers c i (0 ≤ c i ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers ( x i, y i) which represent that characters x i and y i are friends (1 ≤ x i, y i ≤ n, x i ≠ y i). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.
#include
#define ll long long
#define max(a,b) a>b?a:b
#define min(a,b) a
ll fa[100005],c[100005];
ll find(ll x)
{
if(fa[x]==x)
return x;
else
return fa[x]=find(fa[x]);
}
void merge(ll a,ll b)
{
ll af=find(a);
ll bf=find(b);
if(af!=bf)
fa[max(af,bf)]=min(af,bf);
c[min(af,bf)]=min(c[af],c[bf]);
return;
}
int main()
{
ll m,n,a,b,ans=0;
scanf("%lld%lld",&n,&m);
for(ll i=1; i<=n; i++)
{
fa[i]=i;
scanf("%lld",&c[i]);
}
for(ll i=0; i<m; i++)
{
scanf("%lld%lld",&a,&b);
merge(a,b);
}
for(ll i=1; i<=n; i++)
{
if(fa[i]==i)
ans+=c[i];
}
printf("%lld",ans);
return 0;
}
传送门