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;
}
















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值