题目链接:http://codeforces.com/contest/898/problem
A. Rounding
time limit per test 1 second
memory limit per test 256 megabytes
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.
Input
The first line contains single integer n (0 ≤ n ≤ 109) — number that Vasya has.
Output
Print result of rounding n. Pay attention that in some cases answer isn’t unique. In that case print any correct answer.
Examples
input
5
output
0
input
113
output
110
input
1000000000
output
1000000000
input
5432359
output
5432360
Note
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.
题意:题目和名字一样,类似四舍五入,这里比较特别是的最后一位为5的时候可以是舍掉也可以是进位。
#include<bits/stdc++.h>
using namespace std;
int main ()
{
//yyy_3y
//freopen("1.in","r",stdin);
int n; cin >> n;
if (n % 10 <= 5) cout << n- n % 10 <<endl;
else if (n % 10 > 5) cout << n +10 - n % 10 << endl;
}
B. Proper Nutrition
time limit per test 1 second
memory limit per test 256 megabytes
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.
Input
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.
Output
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.
Examples
input
7
2
3
output
YES
2 1
input
100
25
10
output
YES
0 10
input
15
4
8
output
NO
input
9960594
2551
2557
output
YES
1951 1949
Note
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.
题意:x·a + y·b = n,告诉你 n,a,b问是否存在一组x,y,如果存在多组的话就直接输出任意的x,y,不存在输出NO。
思路:暴力O(N)跑一边。这题赛后被hack了,我自己都惊了Σ(っ °Д °;)っ。
发现 i的下标应该从0开始!
#include<bits/stdc++.h>
using namespace std;
int main ()
{
//yyy_3y
// freopen("1.in","r",stdin);
int t,a,b; cin >> t >> a >> b;
for (int i = 0; i <= 10000000; i++){
if (a*i > t) break;
int temp = (t - a*i) / b;
if (a*i + temp*b == t) {
cout << "YES" <<endl;
cout << i << " " <<temp <<endl;
return 0;
}
}
cout <<"NO" <<endl;
return 0;
}
C. Phone Numbers
time limit per test 2 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output
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.
Input
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.
Output
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.
Examples
input
2
ivan 1 00123
masha 1 00123
output
2
masha 1 00123
ivan 1 00123
input
3
karl 2 612 12
petr 1 12
katya 1 612
output
3
katya 1 612
petr 1 12
karl 1 612
input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
output
2
dasha 2 23 789
ivan 4 789 123 2 456
题意:n个人,以及每个人的号码的个数以及电话号码,如果号码a是另一个号码b的后缀,那么a就不记录。
题解:暴力枚举。因为N = 20。 随便暴力,怎么写就是八仙过海,各显神通。
自己写的很丑啊。学习了下隔壁的思路。
#include<bits/stdc++.h>
using namespace std;
map<string,set<string> > mp;
int main ()
{
//yyy_3y
// freopen("1.in","r",stdin);
int n;
while (cin >> n){
while (n--){
string name; cin >> name;
int m; cin >> m;
while (m--){
string num; cin >> num;
mp[name].insert(num);
}
}
}
cout << mp.size() <<endl;
for (auto &it : mp){
vector <string> s;
for (string a : it.second){
bool flag = true;
for (string b : it.second){
if (a == b) continue;
if (b.size() > a.size() && b.substr(b.size() - a.size()) == a) {
flag = false; break;
}
}
if (flag) s.push_back(a);
}
cout << it.first ;
cout << " " << s.size();
for (int i = 0 ; i < s.size(); i++)
cout << " " << s[i];
cout <<endl;
}
return 0;
}
D. Alarm Clock
time limit per test 2 seconds
memory limit per test 256 megabytes
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.
Input
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
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
input
3 3 2
3 5 1
output
1
input
5 10 3
12 8 18 25 1
output
0
input
7 7 2
7 3 4 1 6 5 2
output
6
input
2 2 2
1 3
output
0
Note
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个不同的数字,表示时间,在连续的m分钟内不能有超过k个数,问最少去掉数的个数。
思路:可以用队列进行维护。如果说 队列顶端的值和当前a[i]的值差距已经大于等于m的时候就可以出队列。
#include<bits/stdc++.h>
using namespace std;
int a[1100000];
int main ()
{
//yyy_3y
//freopen("1.in","r",stdin);
int n,m,k; scanf("%d %d %d",&n,&m,&k);
for(int i = 0; i < n; i++) scanf("%d",&a[i]);
sort(a, a + n);
queue<int> q;
int ans = 0;
for (int i = 0; i < n; i++){
while (!q.empty() && a[i] - q.front() >= m) q.pop();
if (q.size() >= k - 1) ans++;
else q.push(a[i]);
}
printf("%d\n",ans);
return 0;
}
E. Squares and not squares
time limit per test 2 seconds
memory limit per test 256 megabytes
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.
Input
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
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.
Examples
input
4
12 14 30 4
output
2
input
6
0 0 0 0 0 0
output
6
input
6
120 110 23 34 25 45
output
3
input
10
121 56 78 81 45 100 1 0 54 78
output
0
Note
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.
题意:2e5个数,可以对每个数字做+1,-1的操作。求最小多少次操作能够到达,n/2 为 平方数 ,n/2 为 非平方数。 (n保证为偶数)
题解:贪心。
用两个优先队列维护一下。q1放 a为平方数的情况。如果a=0,放2,如果a!=0,放1. q2放 非平方数,存距离两边平方数最小的数。
#include<bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q1,q2;
int main ()
{
//yyy_3y
//freopen("1.in","r",stdin);
int n; scanf("%d",&n);
for (int i = 1; i <= n; i++){
int a; scanf("%d",&a);
int sq = sqrt(a);
// printf("sq, a =%d %d\n",sq,a);
if (sq * sq == a) {
if (sq == 0) q1.push(2);
else q1.push(1);
}
else {
int a1 = (sq+1)*(sq+1) - a;
int a2 = a - sq*sq;
q2.push(min(a1,a2));
}
}
long long ans = 0;
// printf("size = %d %d\n",q1.size(),q2.size());
while (n/2 < q1.size()){
ans += q1.top();
q1.pop();
}
while (n/2 < q2.size()){
ans += q2.top();
q2.pop();
}
printf("%lld\n",ans);
return 0;
}