Codeforces Round #274 (Div. 2) A,B,C

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 abc 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 ab and c print the maximum value that you can get.

Input

The input contains three integers ab 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

題目大意:

給三個數,a,b,c 

要求在三個數中插入運算符和括號,使得運算結果最大

但是..不能改變三個數的順序!!!!

沒有看清楚題目!!!

也就是說,(a+c)*b的情況不存在= =

就是因爲這個後來WA了

int main()
{
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    printf("%d\n",max(a+b+c,max(a+b*c,max(a*b+c,max(a*b*c,max((a+b)*c,max((b+c)*a,a*c+b)))))));
    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 ≤ 1001 ≤ 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

題目大意:

給一串長度爲n的序列,和最多k個操作,每次操作只能對序列中某一個數+1,某一個數-1

要求求出序列最終最大值減最小值所能達到的最小值,以及其所需要的最小操作數


解題思路:

這題a了

先排序,然後每次都對最大值和最小值分別加一減一,然後將所得的兩個值重新放到序列中合適的位置。

當操作次數達到k,或者,最大值-最小值<=1時就可以得到答案


ac代碼:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 105
#define maxk 1005

using namespace std;
struct P{
    int a,pre;
};
P p[maxn];
int pre[maxn];
int from[maxk],to[maxk];
int n,k;

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

int main()
{
    scanf("%d %d",&n,&k);
    for(int i=0;i<n;i+=1){
        scanf("%d",&p[i].a);
        p[i].pre=i+1;
    }
    sort(p,p+n,cmp);
    int countt=0;
    while(k--){
        if(p[n-1].a-p[0].a<=1) break;
        from[countt]=p[n-1].pre;
        to[countt++]=p[0].pre;

        p[n-1].a-=1;
        int index=n-1;
        while(p[index].a<p[index-1].a){
            P tmp;
            tmp=p[index];
            p[index]=p[index-1];
            p[index-1]=tmp;
            index-=1;
        }
        p[0].a+=1;
        index=0;
        while(p[index].a>p[index+1].a){
            P tmp;
            tmp=p[index];
            p[index]=p[index+1];
            p[index+1]=tmp;
            index+=1;
        }
    }

    printf("%d %d\n",p[n-1].a-p[0].a,countt);
    for(int i=0;i<countt;i+=1){
        printf("%d %d\n",from[i],to[i]);
    }
    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 2
4 3
output
6

題目大意:

小明準備要考試啦,沒門課有一個規定的考試時間,還有一個提前的考試時間,小明可以在這兩個時間中任意選取一個參加考試

但老師會在考試當天就記錄考試通過時間,並且按照第一個時間記錄

問,如果小明想要讓他的考試通過時間呈非降序排列,他最少需要幾天把所有科目考完?


解題思路:

按照第一個數對數對排序,排完序後掃一遍,如果第i門課的實際考試時間遲於第二門課的第二考試時間,

那麼第二門課就選擇第一考試時間參加考試,否則按照第二考試時間參加考試,


這道題被hack了,原因是沒有注意到第一考試時間相等而第二考試時間不相等的情況,

這組數據

Input:
2
3 2
3 1


Output:
3


Answer:
2



下面是ac代碼

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 5005

using namespace std;
struct P{
    int fir,sec;
}a[maxn];
int n;

int cmp(P a,P b)
{
    if(a.fir==b.fir) return a.sec<b.sec;
    //加了這一句就過了
    return a.fir<b.fir;
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i+=1){
        scanf("%d %d",&a[i].fir,&a[i].sec);
    }
    sort(a,a+n,cmp);
    int check=a[0].sec;
    for(int i=1;i<n;i+=1){
        if(check>a[i].sec) check=a[i].fir;
        else check=a[i].sec;
    }

    printf("%d\n",check);
    return 0;
}
















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章