Codeforces Round #274 (Div. 2)

链接:http://codeforces.com/contest/479

 

A. Expression
time limit per test
1 second
memory limit per test
256 megabytes
 

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 integersa,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 betweena andb, and betweenb andc, 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 andc 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

 

#include <cstdio>
#include <algorithm>
using namespace std;
int max6(int a, int b, int c, int d, int e, int f)
{
    return max(max(max(a,b), max(c,d)),max(e,f));
}
int main()
{
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    int a1 = a + b + c;
    int a2 = a * b + c;
    int a3 = a * (b + c);
    int a4 = a * b * c;
    int a5 = a + (b * c);
    int a6 = (a + b) * c;
    int ans = max6(a1, a2, a3, a4, a5, a6);
    printf("%d
", ans);
}
 


 

B. Towers
time limit per test
1 second
memory limit per test
256 megabytes
 

As you know, all the kids in Berland love playing with cubes. Little Petya hasn towers consisting of cubes of the same size. Tower with numberi consists of ai cubes stacked one on top of the other. Petya defines theinstability 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 thank 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 integersn andk (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 containsn space-separated positive integersai (1 ≤ ai ≤ 104) — the towers' initial heights.

Output

In the first line print two space-separated non-negative integerss andm (m ≤ k). The first number is the value of the minimum possible instability that can be obtained after performing at mostk 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 integersi andj, each of them lies within limits from1 ton. They represent that Petya took the top cube from thei-th tower and put in on thej-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.

 

分析:分是否能整除两种情况,每操作一次就排一次序,由于n不大。不会超时。要特判n=1和a[i]都相等的情况

 

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 1005;
struct Info
{
    int h;
    int pos;
}info[MAX];
int re[5000];

int cmp(Info a, Info b)
{
    return a.h < b.h;
}

int main()
{
    memset(re, 0, sizeof(re));
    int n, k, sum = 0, ave = 0, mod = 0;
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        info[i].pos = i;
        scanf("%d", &info[i].h);
        sum += info[i].h;
    }
    sort(info + 1, info + n + 1, cmp);
    mod = sum % n;
    ave = sum / n;
    int tmp = 0;
    int cnt = 0;
    int cnt2 = 0;
    int ma = info[n].h - info[1].h;
    if(n == 1 || ma == 0)
    {
        printf("0 0
");
        return 0;
    }
    while(1)
    {
        if(mod != 0)
        {
            k--;
            cnt2++;
            info[n].h--;
            info[1].h++;
            re[cnt++] = info[n].pos;
            re[cnt++] = info[1].pos;
            sort(info + 1, info + n + 1, cmp);
            ma = min(ma, info[n].h - info[1].h);
            if(ma == 1 || k == 0)
                break;
        }
        else
        {
            k--;
            cnt2++;
            info[n].h--;
            info[1].h++;
            re[cnt++] = info[n].pos;
            re[cnt++] = info[1].pos;
            sort(info + 1, info + n + 1, cmp);
            ma = min(ma, info[n].h - info[1].h);
            if(ma == 0 || k == 0)
                break;
        }
    }
    printf("%d %d
", ma, cnt2);
    for(int i = 0; i < cnt - 1; i += 2)
        printf("%d %d
", re[i], re[i+1]);
}


 

 

C. Exams
time limit per test
1 second
memory limit per test
256 megabytes
 

Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactlyn 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 thei-th subject on the day numberai. However, Valera has made an arrangement with each teacher and the teacher of thei-th subject allowed him to take an exam before the schedule time on daybi (bi < ai). Thus, Valera can take an exam for thei-th subject either on dayai, or on daybi. 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 numberai.

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 integern (1 ≤ n ≤ 5000) — the number of exams Valera will take.

Each of the next n lines contains two positive space-separated integersai andbi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing thei-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 2
4 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.

分析:先对给定日期排序,同样的话对提前日期排序。优先考虑提前日期

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 5005;
struct Info
{
    int a, b;
}info[MAX];

int cmp(Info x, Info y)
{
    if(x.a == y.a)
        return x.b < y.b;
    return  x.a < y.a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d %d", &info[i].a, &info[i].b);
    sort(info, info + n, cmp);
    int ans = info[0].b;
    for(int i = 1; i < n; i++)
    {
        if(ans <= info[i].b)
            ans = info[i].b;
        else
            ans = info[i].a;
    }
    printf("%d
", ans);
}


 

D. Long Jumps
time limit per test
1 second
memory limit per test
256 megabytes
 

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 isl centimeters. The ruler already hasn marks, with which he can make measurements. We assume that the marks are numbered from 1 ton 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 distancel from the origin. This ruler can be repesented by an increasing sequencea1, a2, ..., an, whereai denotes the distance of thei-th mark from the origin (a1 = 0,an = l).

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

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

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 distancesx andy. 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 integersn,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 thei-th mark to the origin.

Output

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

In the second line print v space-separated integersp1, p2, ..., pv (0 ≤ pi ≤ l). Numberpi means that thei-th mark should be at the distance ofpi 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 230
0 300
Output
2
185 230
Note

In the first sample it is impossible to initially measure the distance of230 centimeters. For that it is enough to add a20 centimeter mark or a230 centimeter mark.

In the second sample you already can use the ruler to measure the distances of185 and230 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.

分析:显然最多加两个。因为l非常大。考虑用set,最简单的三种情况,x和y至少有一个在给定的区间出现,若均未出现则又一次扫一遍,见程序凝视例子便于理解

 

#include<cstdio>
#include<set>
using namespace std;

int main()
{
    int n, l, x, y, get;
    scanf("%d %d %d %d", &n, &l, &x, &y);
    set<int> a;
    a.clear();
    bool flag1 = false, flag2 = false;
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &get);
        a.insert(get);
    }
    int t;
    for(set<int>::iterator it = a.begin(); it != a.end(); it++)
    {
        t = *it;
        if(!flag1 && a.find(t + x) != a.end())
            flag1 = true;
        if(!flag2 && a.find(t + y) != a.end())
            flag2 = true;
    }
    if(flag1 && flag2)
        printf("0
");
    else if(flag1 && !flag2)
        printf("1
%d", y);
    else if(!flag1 && flag2)
        printf("1
%d", x);
    else
    {
        for(set<int>::iterator it = a.begin(); it != a.end(); it++)
        {
            t = *it;
            //4 20 1 4
            //0 6 11 20 插7
            if(a.find(t + x + y) != a.end())
            {
                printf("1
%d
", x + t);
                return 0;
            }
            //4 20 4 5
            //0 6 7 20 插11
            if(a.find(t + y - x) != a.end() && t + y <= l)
            {
                printf("1
%d
", y + t);
                return 0;
            }
            //4 20 3 5
            //0 13 15 20 插10 
            if(a.find(t - y + x) != a.end() && t - y >= 0)
            {
                printf("1
%d
", t - y);
                return 0;
            }
        }
        printf("2
%d %d
", x, y);//若以上都不行,则就插入x和y
    }
}


 


 

E. Riding in a Lift
time limit per test
2 seconds
memory limit per test
256 megabytes
 

Imagine that you are in a building that has exactlyn floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from1 to n. Now you're on the floor numbera. You are very bored, so you want to take the lift. Floor numberb has a secret lab, the entry is forbidden. However, you already are in the mood and decide to makek consecutive trips in the lift.

Let us suppose that at the moment you are on the floor numberx (initially, you were on floor a). For another trip between floors you choose some floor with numbery (y ≠ x) and the lift travels to this floor. As you cannot visit floorb with the secret lab, you decided that the distance from the current floorx to the chosen y must be strictly less than the distance from the current floorx to floor b with the secret lab. Formally, it means that the following inequation must fulfill:|x - y| < |x - b|. After the lift successfully transports you to floory, you write down number y in your notepad.

Your task is to find the number of distinct number sequences that you could have written in the notebook as the result ofk trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by1000000007 (109 + 7).

Input

The first line of the input contains four space-separated integersn, a,b, k (2 ≤ n ≤ 5000,1 ≤ k ≤ 5000, 1 ≤ a, b ≤ n,a ≠ b).

Output

Print a single integer — the remainder after dividing the sought number of sequences by1000000007 (109 + 7).

Sample test(s)
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note

Two sequences p1, p2, ..., pk andq1, q2, ..., qk aredistinct, if there is such integer j (1 ≤ j ≤ k), thatpj ≠ qj.

Notes to the samples:

  1. In the first sample after the first trip you are either on floor1, or on floor 3, because|1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
  2. In the second sample there are two possible sequences:(1, 2); (1, 3). You cannot choose floor3 for the first trip because in this case no floor can be the floor for the second trip.
  3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
#include <cstdio>
#include <algorithm>
using namespace std;
int const MOD = 1e9 + 7;
int const MAX = 5005;
int dp[MAX][MAX], sum[MAX][MAX];

int main() 
{
    int n, a, b, k;
    scanf("%d %d %d %d", &n, &a, &b, &k);
    if(a > b) 
    {
        a = n + 1 - a;
        b = n + 1 - b;
    }
    for(int i = 1; i < b; i++)
    {
        dp[i][0] = 1;
        sum[i][0] = i;
    }
    for(int i = 1; i <= k; i++) 
    {
        for(int j = 1; j < b; j++) 
        {
            int tmp = max(0, 2*j-b);
            dp[j][i] = (sum[b - 1][i - 1] - sum[tmp][i - 1] + MOD) % MOD;
            dp[j][i] = (dp[j][i] - dp[j][i - 1] + MOD) % MOD;
            sum[j][i] = (sum[j - 1][i] + dp[j][i]) % MOD;
        }
    }
    printf("%d
",dp[a][k]);
}


 

原文地址:https://www.cnblogs.com/bhlsheji/p/5074408.html