您的位置:首页 > 其它

Codeforces Round #274 (Div. 2)

2015-10-02 21:07 543 查看
A. Expression

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Petya studies in a school and he adores Maths. His class has been studying arithmetic expressions. On the last class the teacher wrote three positive integers a, b, c on
the blackboard. The task was to insert signs of operations '+' and '*',
and probably brackets between the numbers so that the value of the resulting expression is as large as possible. Let's consider an example: assume that the teacher wrote numbers 1, 2 and 3 on the blackboard. Here are some ways of placing signs and brackets:

1+2*3=7

1*(2+3)=5

1*2*3=6
(1+2)*3=9
Note that you can insert operation signs only between a and b,
and between b and c, that is, you cannot swap integers.
For instance, in the given sample you cannot get expression (1+3)*2.

It's easy to see that the maximum value that you can obtain is 9.

Your task is: given a, b and c print
the maximum value that you can get.

Input

The input contains three integers a, b and c,
each on a single line (1 ≤ a, b, c ≤ 10).

Output

Print the maximum value of the expression that you can obtain.

Sample test(s)

input
1
2
3


output
9


input
2
10
3


output
60


即使题目再简单也不要心急,心急就会wa...

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL  long long
const int MOD = 1e9+7;
using namespace std;

int main()
{
int a,b,c;
while(cin>>a>>b>>c)
{
int d = a+b*c;
int e = a*(b+c);
int f = a*b*c;
int g = (a+b)*c;
int h = a+b+c;
int Max = max(h,max(d,max(max(e,f),g)));
cout<<Max<<endl;
}
return 0;
}


B. Towers

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists
of ai cubes
stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example,
if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2).

The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would
never put the cube on the same tower from which it was removed because he thinks it's a waste of time.

Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him
accomplish this task.

Input

The first line contains two space-separated positive integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1000)
— the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers ai (1 ≤ ai ≤ 104)
— the towers' initial heights.

Output

In the first line print two space-separated non-negative integers s and m (m ≤ k).
The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number
of operations needed for that.

In the next m lines print the description of each operation as two positive integers i and j,
each of them lies within limits from 1 to n. They represent
that Petya took the top cube from the i-th tower and put in on the j-th
one (i ≠ j). Note that in the process of performing operations the heights of some towers can become equal to zero.

If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them.

Sample test(s)

input
3 2
5 8 5


output
0 2
2 1
2 3


input
3 4
2 2 4


output
1 1
3 2


input
5 3
8 3 2 6 3


output
3 3
1 3
1 2
1 3


Note

In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.

以后做cf多測几组数据再交,小数据太水了....这题数据范围小,暴力+排序,每次把最大的移给最小的1个,假设发现移玩以后最大的小于最小的则结束

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include <algorithm>
#define LL  long long
const int MOD = 1e9+7;
using namespace std;
struct node
{
int x;
int id;
}d[200];

struct s
{
int a;
int b;
}p[10005];

bool cmp(node a, node b)
{
return  a.x < b.x;
}
int main()
{
#ifdef xxz
freopen("in","r",stdin);
#endif // xxz
int n,k;
while(cin>>n>>k)
{

for(int i = 1; i <= n; i++)
{
cin>>d[i].x;
d[i].id = i;
}

int cha = 0, cent = 0;
sort(d,d+n+1,cmp);
int m = 0;
for(int i = 0; i < k; i++)
{
if(d[1].x+1 > d
.x-1)
{
cha = d
.x - d[1].x;
break;

}
d[1] .x+= 1;
d
.x-= 1;
cent++;
p[m].a = d
.id;
p[m++].b = d[1].id;
sort(d,d+n+1,cmp);
cha = d
.x- d[1].x;
}
cout<<cha<<" "<<cent<<endl;
for(int i = 0; i < m; i++)
{
cout<<p[i].a<<" "<<p[i].b<<endl;
}
}
return 0;
}


C. Exams

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart
guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.

According to the schedule, a student can take the exam for the i-th subject on the day number ai.
However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai).
Thus, Valera can take an exam for the i-th subject either on day ai,
or on day bi.
All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai.

Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes
exams so that all the records in his record book go in the order of non-decreasing date.

Input

The first line contains a single positive integer n (1 ≤ n ≤ 5000)
— the number of exams Valera will take.

Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109)
— the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.

Output

Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.

Sample test(s)

input
3
5 2
3 1
4 2


output
2


input
3
6 1
5 24 3


output
6


Note

In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes
an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.

In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.

这题题意读懂了就非常easy了,

Valera要參加n场考试,每场考试i,考试规定的时间是ai,但Valera通过老师能够提前到bi(bi<ai),即他既能够ai时參加,也能够bi时參加。Valera想仍依照ai从小到大的顺序參加考试,问最后一场考试时间最早是多少

显然先对ai为第一keyword。bi为第二keyword从小到大排序。然后从小到大扫描,

比方

4 3

5 2
6 1

那么除了第一个取3,其他两个都取5,6假设仅仅排序第一个的话
4 3
4 4
4 1
事实上仅仅要1天就可以,这种话须要4题...不行
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <set>
#include <utility>
#include <ctime>
#include <vector>
using namespace std;

typedef long long ll;

typedef pair<int,int> P;
P p[5010];

bool cmp(P a, P b)
{
if(a.first == b.first)
return a.second < b.second;
return a.first < b.first;
}
int main()
{
#ifdef xxz
freopen("in","r",stdin);
#endif // xxz
int n;
while(cin>>n)
{
for(int i = 0; i <n; i++)
{
cin>>p[i].first>>p[i].second;
}
sort(p,p+n,cmp);
int ans = 0;
for(int i =0; i < n; i++)
{
if(p[i].second >= ans)
ans = max(ans,p[i].second);
else ans = max(ans,p[i].first);
}
cout<<ans<<endl;
}
return 0;
}


D. Long Jumps

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler!

However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has nmarks,
with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end.
The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from
the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an,
where aidenotes
the distance of the i-th mark from the origin (a1 = 0, an = l).

Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n),
such that the distance between the i-th and the j-th
mark is exactly equal to d (in other words, aj - ai = d).

Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y)
centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y.

Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y.
Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler.

Input

The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l)
— the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly.

The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l),
where ai shows
the distance from the i-th mark to the origin.

Output

In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler.

In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l).
Number pi means
that the i-th mark should be at the distance of pi centimeters
from the origin. Print the marks in any order. If there are multiple solutions, print any of them.

Sample test(s)

input
3 250 185 230
0 185 250


output
1
230


input
4 250 185 230
0 20 185 250


output
0


input
2 300 185 2300 300


output
2185 230


Note

In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter
mark or a 230 centimeter mark.

In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters,
so you don't have to add new marks.

In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.

一開始我想使用暴力解决,也就是把尺子点间距离所有算出来,可是须要o(n^2),不行,后来细致想想,这题目答案仅仅会由三种0,1,2,

题目大意:



给出一把尺子。总长度为l。当中有n个标记,当中第一个标记为0, 第n个标记为l。要求量出x和y。如若不能量出,能够在上面添加标记。

要求添加的标记最少。





由题意可知。标记添加量为:0, 1, 2;

0个为x和y都能够在尺子上找到,即a[i]+x和a[i]+y均可在尺子上找到,我们能够採用二分搜索。复杂度为O(nlogn)(能够用STL自带的)

1个为能够在上面标记一个点。使得x和y都能够在尺子上找到,即a[i]+x+y , a[i]+x-y, a[i]-x+y, a[i]+x, a[i]+y。当中一个能够在尺子上找到就能够了。

2个直接标记x,y就够了。

a[i] - a[j] = y-x;

a[i] - a[j = x-y;(存在这样的差的话也能够加一个)


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using  namespace std;
const int maxn = 1e5+10;
int a[maxn];
int n,l,x,y;

bool check_0()
{
int fx,fy;
fx = fy = 0 ;
for(int i = 0; i < n; i++)
{
if(binary_search(a,a+n,a[i]+x) && a[i]+x >=0 && a[i]+x <= l)
{
fx = 1;
}

if(binary_search(a,a+n,a[i]+y) && a[i]+y >= 0 && a[i] + y <= l)
{
fy = 1;
}

}

if(fx && fy)  return true;
else return false;
}

bool check_1()
{
for(int i = 0; i < n; i++)
{
if(binary_search(a,a+n,a[i]+x) && y >=0 && y <= l)
{
cout<<1<<endl<<y<<endl;
return true;
}

if(binary_search(a,a+n,a[i]+y) && x >= 0 && x <= l)
{
cout<<1<<endl<<x<<endl;
return true;
}

if(binary_search(a,a+n,a[i]+y-x) && a[i] -x >= 0 && a[i] -x <= l)
{
cout<<1<<endl<<a[i]-x<<endl;

return true;
}

if(binary_search(a,a+n,a[i]+x-y) && a[i]+x >= 0 && a[i] +  x <= l)
{
cout<<1<<endl<<a[i]+x;
return true;
}

if(binary_search(a,a+n,a[i]+y+x) && a[i]+y+x >= 0 && a[i] + y+x <= l)
{
cout<<1<<endl<<a[i]+x<<endl;
return true;
}
}
return false;
}

int main()
{

#ifdef xxz
freopen("in","r",stdin);
#endif // xxz
while(cin>>n>>l>>x>>y)
{
for(int i = 0; i < n; i++)
{
cin>>a[i];
}

if(check_0())
{
cout<<0<<endl;
}
else if(!check_1())
{
cout<<2<<endl<<x<<" "<<y<<endl;
}
}
return 0;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: