Codeforces Round #274(Div.2) 题解

Codeforces Round #274 (Div.2)

A.Expression

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.
Examples

Input
1 2 3
Output
9

题目大意:

在三个数字之间任意加括号,加号,乘号,求最大值。

大概思路:

因为只有六种情况,所以暴力出奇迹~

代码:

#include<iostream>
using namespace std;

int main()
{
    int a, b, c;
    cin>>a>>b>>c;

    int t = a*b*c;
    t = max(t, a+b*c);
    t = max(t, (a+b)*c);
    t = max(t, a*b+c);
    t = max(t, a*(b+c));
    t = max(t, a+b+c);
    cout<<t<<endl;
    return 0;
}

B.Towers

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 a i a_i (1 ≤  a i a_i  ≤ 1 0 4 10^4 ) — 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.
Examples

Input
3 2
5 8 5
Output
0 2
2 1
2 3

题目大意:

在k次操作内,让n个数的极差最小

大概思路:

首先将n个数降序(或升序),在k次循环里,第一个数-1, 最后一个数+1,然后再降序,方便每次操作直接拿数组两头的元素相减。如果在k次操作内所有都相等,结束循环。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
 
const int MAXN = 1000+10;
int n,k,ans;
int st[MAXN][MAXN];//二维数组存储每次操作两个数的初始位置
 
struct point//存ai和i
{
    int x,w;
}p[MAXN];
 
int cmp(point a,point b)//降序排列
{
    return a.w>b.w;
}
 
int main(){
    //freopen("input.txt","r",stdin);//从文档读入输入数据
    while(~scanf("%d%d",&n,&k))
    {
        for(int i = 0; i < n; ++i)
        {
            scanf("%d",&p[i].w);
            p[i].x = i + 1;
        }
        sort(p,p+n,cmp);
        ans = p[0].w-p[n-1].w;
        int cnt = 0;
        while(k--)
        {
            if(ans == 0) break;//所有值相等就提前结束循环
            p[0].w--;
            p[n-1].w++;
            st[cnt][0] = p[0].x;
            st[cnt][1] = p[n-1].x;
            sort(p,p+n,cmp);
            ans = p[0].w-p[n-1].w;
            ++cnt;//记录操作次数
            if(ans <= 1) break;
        }
        printf("%d %d\n",ans,cnt);
        for(int i = 0; i < cnt; ++i)
            printf("%d %d\n",st[i][0],st[i][1]);
    }
    return 0;
}

C.Exams

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 a i a_i . 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 b i b_i ( b i b_i  <  a i a_i ). Thus, Valera can take an exam for the i-th subject either on day a i a_i , or on day b i b_i . 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 a i a_i .

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 a i a_i and b i b_i (1 ≤  b i b_i  <  a i a_i  ≤  1 0 9 10^9 ) — 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.
Examples

Input
3
5 2
3 1
4 2
Output
2

题目大意:

某大学生的期末考试要来了,他的每一门考试都有一个实际时间和提前时间,求如何按实际考试时间的升序尽量早点考完

大概思路:

用结构数组存好每一门考试的实际时间和提前时间,将实际时间由小到大排序好,当实际时间相同时,提前时间小的排前面,然后循环判断能不能选提前时间(具体看代码)

代码

#include<iostream>
#include<algorithm>
using namespace std;

struct point
{
    int a, b;
}point[50001];

bool cmp(struct point m,struct point n)
{
    if(m.a != n.a)
        return m.a < n.a;
    return m.b < n.b;//实际时间相等提前时间小的排前面
}

int main()
{     
    int n;
    cin>>n;

    for(int i = 0; i < n; i++)
        cin>>point[i].a>>point[i].b;
    sort(point, point+n, cmp);//给结构体排序

    int day = point[0].b;//第一门考试时间为最早的那门考试的提前时间
    for(int i = 1; i < n; i++)
    {
        if(point[i].b >= day)//提前时间大于上一门考试时间就能提前
            day = point[i].b;
        else
            day= point[i].a;//否则不能提前考
    }
    cout<<day<<endl;
    return 0;
}

D.Long Jumps

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 n marks, 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 a 1 a_1 , a 2  a_2 , …,  a n a_n , where a i a_i denotes the distance of the i-th mark from the origin ( a 1 a_1  = 0, a n a_n  = 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, a j a_j  -  a i a_i  = i).

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 a 1 a_1 ,  a 2 a_2 , …,  a n a_n (0 =  a 1 a_1  < a 2  a_2  < … <  a n a_n  = 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 p 1 p_1 ,  p 2 p_2 , …,  p v p_v (0 ≤  p i p_i  ≤ l). Number p i p_i 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.

Examples

Input
3 250 185 230
0 185 250
Output
1
230

题目大意:

一根尺子能不能量出两个数x和y,如果不能,最少要在尺子上添加几个刻度。

大概思路:

先找到两个数x和y有几个是能量出来的,即尺子上会不会有某两个数的间距为x或y,如果都能找到就输出0,有一个没找到直接输出这个数,如果两个都没找到,不能认为答案为2,因为有可能只要添加一个刻度,就能形成大小等于另外一个数的间距(也只有这种情况需要着重讨论)

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
const int maxn = 1e5+5;
int N, L,  X, Y, A[maxn];
 
bool judge (int u) {
    if (u < 0 || u > L) return false;
    int k = lower_bound(A, A + N, u) - A;
    return u == A[k];
}
 
void solve () {
    int ans = 0;
    for (int i = 0; i < N; i++) {
        if (judge(A[i] - X) || judge(A[i] + X))
            ans |= 1;
        if (judge(A[i] - Y) || judge(A[i] + Y))
            ans |= 2;
    }
 
    if (ans == 3)
        printf("0\n");
    else if (ans == 2)
        printf("1\n%d\n", X);
    else if (ans == 1)
        printf("1\n%d\n", Y);
    else {
 
        for (int i = 0; i < N; i++) {
            int tx = A[i] + X;
            int ty = A[i] + Y;
 
            if (tx <= L && (judge(tx - Y) || judge(tx + Y))) {
                printf("1\n%d\n", tx);
                return;
            }
 
            if (ty <= L && (judge(ty - X) || judge(ty + X))) {
                printf("1\n%d\n", ty);
                return;
            }
        }
 
        for (int i = 0; i < N; i++) {
            int tx = A[i] - X;
            int ty = A[i] - Y;
 
            if (tx >= 0 && (judge(tx - Y) || judge(tx + Y))) {
                printf("1\n%d\n", tx);
                return;
            }
 
            if (ty >= 0 && (judge(ty - X) || judge(ty + X))) {
                printf("1\n%d\n", ty);
                return;
            }
        }
        printf("2\n%d %d\n", X, Y);
    }
}
 
int main () {
    scanf("%d%d%d%d", &N, &L, &X, &Y);
    for (int i = 0; i < N; i++)
        scanf("%d", &A[i]);
    solve();
    return 0;
}

E.Riding in a Lift

还不会…下面提供几个有比较高浏览量的题解?

答案一号
答案二号
答案三号

发布了26 篇原创文章 · 获赞 11 · 访问量 2318

猜你喜欢

转载自blog.csdn.net/qq_41731507/article/details/86937538
今日推荐