Vasya has a non-negative integer n. He wants to round it to nearest integer, which ends up with 0. If n already ends up with 0, Vasya considers it already rounded.
For example, if n = 4722 answer is 4720. If n = 5 Vasya can round it to 0 or to 10. Both ways are correct.
For given n find out to which integer will Vasya round it.
The first line contains single integer n (0 ≤ n ≤ 109) — number that Vasya has.
Print result of rounding n. Pay attention that in some cases answer isn't unique. In that case print any correct answer.
5
0
113
110
1000000000
1000000000
5432359
5432360
In the first example n = 5. Nearest integers, that ends up with zero are 0 and 10. Any of these answers is correct, so you can print 0 or 10.
题意:找出离n最近的一个数满足他最后一位是0。
解:把最后一位x取出来,小于等于5就减去x,不然加上10-x。


1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int main() 5 { 6 scanf("%d",&n); 7 int x=n%10; 8 if(x<=5){ 9 n-=x; 10 } 11 else { 12 n+=10-x; 13 } 14 printf("%d\n",n); 15 return 0; 16 }
Vasya has n burles. One bottle of Ber-Cola costs a burles and one Bars bar costs b burles. He can buy any non-negative integer number of bottles of Ber-Cola and any non-negative integer number of Bars bars.
Find out if it's possible to buy some amount of bottles of Ber-Cola and Bars bars and spend exactly n burles.
In other words, you should find two non-negative integers x and y such that Vasya can buy x bottles of Ber-Cola and y Bars bars and x·a + y·b = n or tell that it's impossible.
First line contains single integer n (1 ≤ n ≤ 10 000 000) — amount of money, that Vasya has.
Second line contains single integer a (1 ≤ a ≤ 10 000 000) — cost of one bottle of Ber-Cola.
Third line contains single integer b (1 ≤ b ≤ 10 000 000) — cost of one Bars bar.
If Vasya can't buy Bars and Ber-Cola in such a way to spend exactly n burles print «NO» (without quotes).
Otherwise in first line print «YES» (without quotes). In second line print two non-negative integers x and y — number of bottles of Ber-Cola and number of Bars bars Vasya should buy in order to spend exactly n burles, i.e. x·a + y·b = n. If there are multiple answers print any of them.
Any of numbers x and y can be equal 0.
7
2
3
YES
2 1
100
25
10
YES
0 10
15
4
8
NO
9960594
2551
2557
YES
1951 1949
In first example Vasya can buy two bottles of Ber-Cola and one Bars bar. He will spend exactly 2·2 + 1·3 = 7 burles.
In second example Vasya can spend exactly n burles multiple ways:
- buy two bottles of Ber-Cola and five Bars bars;
- buy four bottles of Ber-Cola and don't buy Bars bars;
- don't buy Ber-Cola and buy 10 Bars bars.
In third example it's impossible to but Ber-Cola and Bars bars in order to spend exactly n burles.
题意:给定n,a,b,求出x,y,使得ax+by=n。
解:枚举x,判断一下存不存在y。


1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,a,b; 4 int main() 5 { 6 scanf("%d%d%d",&n,&a,&b); 7 for(int i=0;i*a<=n;i++){ 8 if((n-i*a)%b==0){ 9 printf("YES\n%d %d\n",i,(n-i*a)/b); 10 return 0; 11 } 12 } 13 printf("NO\n"); 14 return 0; 15 }
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings — all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
First line contains the integer n (1 ≤ n ≤ 20) — number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Print out the ordered information about the phone numbers of Vasya's friends. First output m — number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
2
ivan 1 00123
masha 1 00123
2
masha 1 00123
ivan 1 00123
3
karl 2 612 12
petr 1 12
katya 1 612
3
katya 1 612
petr 1 12
karl 1 612
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
2
dasha 2 23 789
ivan 4 789 123 2 456
题意:给定若干行,每一行代表一个本上记录的信息,格式为:人名+电话号码个数+所有电话号码。每一个本上貌似只有一个人。让你筛选出每个人的所有电话号码是多少,
如果同一个人给定的两个号码a,b,满足a是b的后缀,那么不输出a。
解:数据范围很小,set判一下重,模拟即可。


1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,cnt; 4 map<string,int>S; 5 struct pre{ 6 int ans; 7 set<string>G; 8 bool b[205]; 9 string nam; 10 string s[205]; 11 bool cmp(int x,int y){ 12 int lenx=s[x].size(); 13 int leny=s[y].size(); 14 if(lenx>=leny)return 0; 15 for(int i=leny,j=lenx;j!=-1;i--,j--){ 16 if(s[x][j]!=s[y][i])return 0; 17 } 18 return 1; 19 } 20 void work(){ 21 for(int i=1;i<=ans;i++){ 22 for(int j=1;j<=ans;j++){ 23 if(cmp(i,j))b[i]=1; 24 } 25 } 26 int tem=0; 27 for(int i=1;i<=ans;i++){ 28 if(!b[i])tem++; 29 } 30 cout<<tem<<" "; 31 for(int i=1;i<=ans;i++){ 32 if(!b[i])cout<<s[i]<<" "; 33 } 34 cout<<endl; 35 } 36 }p[21]; 37 int main() 38 { 39 scanf("%d",&n); 40 for(int i=1;i<=n;i++){ 41 string s; 42 int x; 43 cin>>s>>x; 44 if(S.find(s)==S.end()){ 45 S[s]=++cnt; 46 p[cnt].nam=s; 47 p[cnt].ans=x; 48 for(int i=1;i<=x;i++){ 49 string u; 50 cin>>u; 51 if(p[cnt].G.find(u)!=p[cnt].G.end())continue; 52 p[cnt].G.insert(u); 53 p[cnt].s[i]=u; 54 } 55 } 56 else { 57 int t=S[s]; 58 for(int i=1;i<=x;i++){ 59 string u; 60 cin>>u; 61 if(p[t].G.find(u)!=p[t].G.end())continue; 62 p[t].G.insert(u); 63 p[t].s[++p[t].ans]=u; 64 } 65 } 66 } 67 cout<<cnt<<endl; 68 for(int i=1;i<=cnt;i++){ 69 cout<<p[i].nam<<" "; 70 p[i].work(); 71 } 72 return 0; 73 }
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
3 3 2
3 5 1
1
5 10 3
12 8 18 25 1
0
7 7 2
7 3 4 1 6 5 2
6
2 2 2
1 3
0
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
题意:有n个闹钟,分别在第ai开始响,持续一分钟,问最少关掉几个闹钟,可以保证不存在连续的m分钟有k个闹钟开始响。
解:从左到右用一个单调队列维护一下进出,单调队列长度为m-1,一旦遇到不合法就关掉当前这个闹钟,因为关后面的肯定不会比关前面的更差,所以贪心正确。


1 #include<bits/stdc++.h> 2 using namespace std; 3 const int inf=2e5+1; 4 int a[inf],n,m,k,ans; 5 int q[inf],head=1,tail; 6 int main() 7 { 8 scanf("%d%d%d",&n,&m,&k); 9 for(int i=1;i<=n;i++)scanf("%d",&a[i]); 10 sort(a+1,a+n+1); 11 for(int i=1;i<=n;i++){ 12 while(head<=tail&&a[q[head]]+m-1<a[i])head++; 13 if(tail-head+1==k-1){ 14 ans++; 15 continue; 16 } 17 else q[++tail]=i; 18 } 19 printf("%d\n",ans); 20 return 0; 21 }
Ann and Borya have n piles with candies and n is even number. There are ai candies in pile with number i.
Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this candy is new and doesn't belong to any other pile) or remove one candy (if there is at least one candy in this pile).
Find out minimal number of moves that is required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer.
First line contains one even integer n (2 ≤ n ≤ 200 000) — number of piles with candies.
Second line contains sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 109) — amounts of candies in each pile.
Output minimal number of steps required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer. If condition is already satisfied output 0.
4
12 14 30 4
2
6
0 0 0 0 0 0
6
6
120 110 23 34 25 45
3
10
121 56 78 81 45 100 1 0 54 78
0
In first example you can satisfy condition in two moves. During each move you should add one candy to second pile. After it size of second pile becomes 16. After that Borya and Ann will have two piles with number of candies which is a square of integer (second and fourth pile) and two piles with number of candies which is not a square of any integer (first and third pile).
In second example you should add two candies to any three piles.
题意:有n堆糖,保证n是偶数,每一次操作可以选择一堆+1个糖或者-1个糖,问最少多少次操作之后,可以使得n/2堆是完全平方数,n/2堆不是。
解:对于初始的每一堆的个数,判断他是不是完全平方数,并求出改变他当前“属性”最少要多少次(完全->非完全,非完全->完全),记录次数w。
记录非完全平方数的个数x,判断与n/2的大小关系,大于则把w前x-n/2小的非完全平方数变成完全平方数,小于则把w前n/2-x小的完全平方数变成
非完全平方数。


1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int inf=2e5+1; 5 int n,a[inf]; 6 ll ans; 7 struct pre{ 8 int w,d; 9 bool operator < (const pre &o)const { 10 return d<o.d||(d==o.d&&w<o.w); 11 } 12 }p[inf]; 13 int main() 14 { 15 scanf("%d",&n); 16 for(int i=1;i<=n;i++)scanf("%d",&a[i]); 17 for(int i=1;i<=n;i++){ 18 int x=sqrt(a[i]); 19 if(x*x==a[i]){ 20 p[i].d=1; 21 if(!a[i])p[i].w=2; 22 else p[i].w=1; 23 } 24 else { 25 p[i].d=0; 26 p[i].w=min(a[i]-x*x,(x+1)*(x+1)-a[i]); 27 } 28 } 29 sort(p+1,p+n+1); 30 int cnt=0; 31 for(int i=1;i<=n;i++)if(!p[i].d)cnt++; 32 if(cnt<n/2){ 33 int k=n/2-cnt; 34 for(int i=1;k;i++){ 35 if(!p[i].d)continue; 36 k--; 37 ans+=p[i].w; 38 } 39 } 40 else { 41 int k=cnt-n/2; 42 for(int i=1;i<=k;i++)ans+=p[i].w; 43 } 44 printf("%lld\n",ans); 45 return 0; 46 }