Codeforces Round #158 (Div. 2
A. Adding Digits
A. Adding Digits
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya has got two number: a and b. However, Vasya finds number a too short. So he decided to repeat the operation of lengthening number a n times.
One operation of lengthening a number means adding exactly one digit to the number (in the decimal notation) to the right provided that the resulting number is divisible by Vasya’s number b. If it is impossible to obtain the number which is divisible by b, then the lengthening operation cannot be performed.
Your task is to help Vasya and print the number he can get after applying the lengthening operation to number a n times.
Input
The first line contains three integers: a, b, n (1 ≤ a, b, n ≤ 105).
Output
In a single line print the integer without leading zeros, which Vasya can get when he applies the lengthening operations to number a n times. If no such number exists, then print number -1. If there are multiple possible answers, print any of them.
Examples
inputCopy
5 4 5
outputCopy
524848
inputCopy
12 11 1
outputCopy
121
inputCopy
260 150 10
outputCopy
-1
题意:给定a,b两个数,在n次操作步骤范围内,每次可以在a的右侧增加0~9任意一个数,使得最后的结果‘a’可以使a%b=0
题解:本题显而易见的可以看出,是对于数字的可能性的模拟。显然我们对于所有n次的0~9的数字的可能性进行搜索亦或者暴力,这样的组合情况一定会导致TLE的,如果我们对于第一个增添的数进行模拟的话,如果存在a%b==0,那么一定会存在n次结果后的a%b=0,反之,则不存在这样的a%b=0.
#include<bits/stdc++.h>
using namespace std;
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
int main()
{
FAST;
int a,b,n;
cin>>a>>b>>n;
bool flag=false;//标志a%b==0是否存在
int num;//新的a的数字的构造
for(int i=0;i<=9;i++)
{
num=a*10+i;
if(num%b==0)
{
flag=true;
break;
}
}
if(!flag)//不存在符合题意的a
{
cout<<-1<<endl;
return 0;
}
cout<<num;
for(int i=1;i<n;i++)//剩下的n-1位可以认为是b的若干倍
{
cout<<0;
}
cout<<endl;
return 0;
}
B. Ancient Prophesy
B. Ancient Prophesy
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A recently found Ancient Prophesy is believed to contain the exact Apocalypse date. The prophesy is a string that only consists of digits and characters “-”.
We’ll say that some date is mentioned in the Prophesy if there is a substring in the Prophesy that is the date’s record in the format “dd-mm-yyyy”. We’ll say that the number of the date’s occurrences is the number of such substrings in the Prophesy. For example, the Prophesy “0012-10-2012-10-2012” mentions date 12-10-2012 twice (first time as “0012-10-2012-10-2012”, second time as “0012-10-2012-10-2012”).
The date of the Apocalypse is such correct date that the number of times it is mentioned in the Prophesy is strictly larger than that of any other correct date.
A date is correct if the year lies in the range from 2013 to 2015, the month is from 1 to 12, and the number of the day is strictly more than a zero and doesn’t exceed the number of days in the current month. Note that a date is written in the format “dd-mm-yyyy”, that means that leading zeroes may be added to the numbers of the months or days if needed. In other words, date “1-1-2013” isn’t recorded in the format “dd-mm-yyyy”, and date “01-01-2013” is recorded in it.
Notice, that any year between 2013 and 2015 is not a leap year.
Input
The first line contains the Prophesy: a non-empty string that only consists of digits and characters “-”. The length of the Prophesy doesn’t exceed 105 characters.
Output
In a single line print the date of the Apocalypse. It is guaranteed that such date exists and is unique.
Examples
inputCopy
777-444—21-12-2013-12-2013-12-2013—444-777
outputCopy
13-12-2013
题意:寻找十位字符串的日期并且该日期在所给定的字符串中出现的次数最多。
题解:次数利用map进行相关的统计,题目中的数字范围比较小,进行一重的暴力循环即可解决本题。----------字符串中的字符可能是数字也可能是字符,在c++的库中存在isdigit()函数进行相关的数字的判断,便于对于不符合情况的解直接排除。
#include<bits/stdc++.h>
using namespace std;
map<int ,map<int ,map<int ,int > > >mp;
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//2013~2015年均不是闰年,所以二月就只能是28天
const int N=1e5+10;
char a[N];
int dd,mm,yy,ans;
int main()
{
scanf("%s",a);
int len = strlen(a);
for(int i=0;i+9<len;i++)
{
if(isdigit(a[i])&&isdigit(a[i+1])&&isdigit(a[i+3])&&isdigit(a[i+4])
&&isdigit(a[i+6])&&isdigit(a[i+7])&&isdigit(a[i+8])&&isdigit(a[i+9])
&&a[i+2]=='-'&&a[i+5]=='-')
{
int d=(a[i]-'0')*10+(a[i+1]-'0');
int m=(a[i+3]-'0')*10+(a[i+4]-'0');
int y=(a[i+6]-'0')*1000+(a[i+7]-'0')*100+(a[i+8]-'0')*10+a[i+9]-'0';
if(y>=2013&&y<=2015&&m>=1&&m<=12&&d>=1&&d<=day[m])//对于日期的规范性的约束条件
{
mp[y][m][d]++;
if(mp[y][m][d]>ans)//进行唇线次数最多的日期的计数
{
ans=mp[y][m][d];
yy=y,mm=m,dd=d;
}
}
}
}
printf("%02d-%02d-%04d\n",dd,mm,yy);//输出即可
return 0;
}
C. Balls and Boxes
C. Balls and Boxes
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.
Once Vasya chose one of the boxes, let’s assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on.
For example, let’s suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya’s actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.
At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls.
He asks you to help to find the initial arrangement of the balls in the boxes.
Input
The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, …, an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
Examples
inputCopy
4 4
4 3 1 6
outputCopy
3 2 5 4
inputCopy
5 2
3 2 0 2 7
outputCopy
2 1 4 1 6
inputCopy
3 3
2 3 1
outputCopy
1 2 3
题解:本题就是求原数组的各个位置的数字值。
不难发现,进行相关的分配小球,那么一定会使被分配的那个位置的数组的值在整个数组的值为最小
#include<bits/stdc++.h>
using namespace std;
#define INF (ll)1e60
#define ll long long
int read()
{
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int n,m;
ll a[100005],t=INF;
int main()
{
n=read();m=read();
for(int i=1;i<=n;i++)a[i]=read(),t=min(t,a[i]);
for(int i=1;i<=n;i++)a[i]-=t;
ll s=n*t;
while(a[m])
{
a[m]--;
m--;s++;
if(m==0)m=n;
}
a[m]=s;
for(int i=1;i<=n;i++)cout<<a[i]<<' ';
return 0;
}
D. Black and White Tree
D. Black and White Tree
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
The board has got a painted tree graph, consisting of n nodes. Let us remind you that a non-directed graph is called a tree if it is connected and doesn’t contain any cycles.
Each node of the graph is painted black or white in such a manner that there aren’t two nodes of the same color, connected by an edge. Each edge contains its value written on it as a non-negative integer.
A bad boy Vasya came up to the board and wrote number sv near each node v — the sum of values of all edges that are incident to this node. Then Vasya removed the edges and their values from the board.
Your task is to restore the original tree by the node colors and numbers sv.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 105) — the number of nodes in the tree. Next n lines contain pairs of space-separated integers ci, si (0 ≤ ci ≤ 1, 0 ≤ si ≤ 109), where ci stands for the color of the i-th vertex (0 is for white, 1 is for black), and si represents the sum of values of the edges that are incident to the i-th vertex of the tree that is painted on the board.
Output
Print the description of n - 1 edges of the tree graph. Each description is a group of three integers vi, ui, wi (1 ≤ vi, ui ≤ n, vi ≠ ui, 0 ≤ wi ≤ 109), where vi and ui — are the numbers of the nodes that are connected by the i-th edge, and wi is its value. Note that the following condition must fulfill cvi ≠ cui.
It is guaranteed that for any input data there exists at least one graph that meets these data. If there are multiple solutions, print any of them. You are allowed to print the edges in any order. As you print the numbers, separate them with spaces.
Examples
inputCopy
3
1 3
1 2
0 5
outputCopy
3 1 3
3 2 2
inputCopy
6
1 0
0 3
1 8
0 2
0 3
0 0
outputCopy
2 3 3
5 3 3
4 3 2
1 6 0
2 1 0
题意:根据给定的点进行相关的构造所求的树。
本题给了两种点集--------黑点集和白点集,从所给样例可以看出两种集合的度的总和是相等的。将两色的结点排序后,依次贪心构造,是为了更好的进行相关的n-1条边的构造
每次取出来的两个结点用小的那个作为权值构造一条边,然后删去一个结点
当有其中一个集合为空时,剩下所有的结点只要连一条权值为0的边到另一种颜色的任一结点即可。
#include<bits/stdc++.h>
using namespace std;
#define pa pair<int,int>
#define ll long long
int read()
{
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int n;
vector<pa> v[2];//存放黑白两点的度
int main()
{
n=read();
for(int i=1;i<=n;i++)
{
int c=read(),s=read();
v[c].push_back(make_pair(s,i));
}
sort(v[0].begin(),v[0].end());
sort(v[1].begin(),v[1].end());
for(int i=0,j=0;i<v[0].size()&&j<v[1].size();)
{
int t=min(v[0][i].first,v[1][j].first);
printf("%d %d %d\n",v[0][i].second,v[1][j].second,t);
v[0][i].first-=t;v[1][j].first-=t;
if(v[0][i].first)j++;//另外一种的配对后移
else if(v[1][j].first)i++;//第一种的配对后移
else if(i+1<v[0].size())i++;//这种情况一般就是两个点之间的权值为0,并且数的构造不能超过的点集的大小
else j++;
}
return 0;
}
E. Dividing Kingdom
E. Dividing Kingdom
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A country called Flatland is an infinite two-dimensional plane. Flatland has n cities, each of them is a point on the plane.
Flatland is ruled by king Circle IV. Circle IV has 9 sons. He wants to give each of his sons part of Flatland to rule. For that, he wants to draw four distinct straight lines, such that two of them are parallel to the Ox axis, and two others are parallel to the Oy axis. At that, no straight line can go through any city. Thus, Flatland will be divided into 9 parts, and each son will be given exactly one of these parts. Circle IV thought a little, evaluated his sons’ obedience and decided that the i-th son should get the part of Flatland that has exactly ai cities.
Help Circle find such four straight lines that if we divide Flatland into 9 parts by these lines, the resulting parts can be given to the sons so that son number i got the part of Flatland which contains ai cities.
Input
The first line contains integer n (9 ≤ n ≤ 105) — the number of cities in Flatland. Next n lines each contain two space-separated integers: xi, yi ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th city. No two cities are located at the same point. The last line contains nine space-separated integers: .
Output
If there is no solution, print a single integer -1.
Otherwise, print in the first line two distinct real space-separated numbers: x1, x2 — the abscissas of the straight lines that are parallel to the Oy axis. And in the second line print two distinct real space-separated numbers: y1, y2 — the ordinates of the straight lines, parallel to the Ox. If there are multiple solutions, print any of them.
When the answer is being checked, a city is considered to lie on a straight line, if the distance between the city and the line doesn’t exceed 10 - 6. Two straight lines are considered the same if the distance between them doesn’t exceed 10 - 6.
Examples
inputCopy
9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
1 1 1 1 1 1 1 1 1
outputCopy
1.5000000000 2.5000000000
1.5000000000 2.5000000000
inputCopy
15
4 4
-1 -3
1 5
3 -4
-4 4
-1 1
3 -3
-4 -5
-3 3
3 2
4 1
-4 2
-2 -5
-3 4
-1 4
2 1 2 1 2 1 3 2 1
outputCopy
-3.5000000000 2.0000000000
3.5000000000 -1.0000000000
inputCopy
10
-2 10
6 0
-16 -6
-4 13
-4 -2
-17 -10
9 15
18 16
-5 2
10 -5
2 1 1 1 1 1 1 1 1
outputCopy
-1
Note
The solution for the first sample test is shown below:
The solution for the second sample test is shown below:
There is no solution for the third sample test.
这个先空着,想通了再写吧