Codeforces Round #251 (Div. 2) A/B/D

畫船聽雨發表於2014-06-05

A。水題。

A. Devu, the Singer and Churu, the Joker
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Devu is a renowned classical singer. He is invited to many big functions/festivals. Recently he was invited to "All World Classical Singing Festival". Other than Devu, comedian Churu was also invited.

Devu has provided organizers a list of the songs and required time for singing them. He will sing n songs, ith song will take ti minutes exactly.

The Comedian, Churu will crack jokes. All his jokes are of 5 minutes exactly.

People have mainly come to listen Devu. But you know that he needs rest of 10 minutes after each song. On the other hand, Churu being a very active person, doesn't need any rest.

You as one of the organizers should make an optimal sсhedule for the event. For some reasons you must follow the conditions:

  • The duration of the event must be no more than d minutes;
  • Devu must complete all his songs;
  • With satisfying the two previous conditions the number of jokes cracked by Churu should be as many as possible.

If it is not possible to find a way to conduct all the songs of the Devu, output -1. Otherwise find out maximum number of jokes that Churu can crack in the grand event.

Input

The first line contains two space separated integers nd (1 ≤ n ≤ 100; 1 ≤ d ≤ 10000). The second line contains n space-separated integers: t1, t2, ..., tn (1 ≤ ti ≤ 100).

Output

If there is no way to conduct all the songs of Devu, output -1. Otherwise output the maximum number of jokes that Churu can crack in the grand event.

Sample test(s)
input
3 30
2 2 1
output
5
input
3 20
2 1 1
output
-1
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x7f3f3ff
#define PI 3.1415926535898


const int maxn = 10010;

using namespace std;

int main()
{
    int n, d;
    while(cin >>n>>d)
    {
        int sum = 0;
        int x;
        for(int i = 1; i <= n; i++)
        {
            cin >>x;
            sum += x;
        }
        sum += (n-1)*10;
        if(sum <= d)
        {
            int t = (n-1)*2+(d-sum)/5;
            cout<<t<<endl;
        }
        else
            cout<<"-1"<<endl;
    }
    return 0;
}

B.注意資料型別啊,int*int即使你用long long 存,也是int、、然後就悲劇了啊、、細節啊

B. Devu, the Dumb Guy
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the ith subject has ci chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously.

Let us say that his initial per chapter learning power of a subject is x hours. In other words he can learn a chapter of a particular subject in x hours.

Well Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly). Note that his per chapter learning power can not be less than 1 hour.

You can teach him the n subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all the subjects and you will be free to do some enjoying task rather than teaching a dumb guy.

Please be careful that answer might not fit in 32 bit data type.

Input

The first line will contain two space separated integers nx (1 ≤ n, x ≤ 105). The next line will contain n space separated integers:c1, c2, ..., cn (1 ≤ ci ≤ 105).

Output

Output a single integer representing the answer to the problem.

Sample test(s)
input
2 3
4 1
output
11
input
4 2
5 1 2 1
output
10
input
3 3
1 1 1
output
6
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x7f3f3ff
#define PI 3.1415926535898


const int maxn = 1000010;

using namespace std;

LL num[maxn];

int main()
{
    LL n, m;
    while(cin >>n>>m)
    {
        for(int i = 0; i < n; i++)
            cin >>num[i];
        sort(num, num+n);
        LL cnt = 0;
        for(int i = 0; i < n; i++)
        {
            cnt += num[i]*m;
            if(m > 1)
                m--;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

D。這隻能說是太年輕啊、、、終於遇見了會的題目了啊,感覺什麼都沒問題,無奈三分怎麼寫都不對,後來發現要牽扯到實數的列舉啊、、發現了兩個函式:

floor 功 能: 返回小於或者等於指定表示式的最大整數 用 法: double floor(double x) 

ceil  功 能: 返回大於或者等於指定表示式的最小整數  用法:  double ceil(double x);

D. Devu and his Brother
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays a and bby their father. The array a is given to Devu and b to his brother.

As Devu is really a naughty kid, he wants the minimum value of his array a should be at least as much as the maximum value of his brother's array b.

Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times.

You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting.

Input

The first line contains two space-separated integers nm (1 ≤ n, m ≤ 105). The second line will contain n space-separated integers representing content of the array a (1 ≤ ai ≤ 109). The third line will contain m space-separated integers representing content of the array b (1 ≤ bi ≤ 109).

Output

You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.

Sample test(s)
input
2 2
2 3
3 5
output
3
input
3 2
1 2 3
3 4
output
4
input
3 2
4 5 6
1 2
output
0
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-4
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x7ffffff
#define PI 3.1415926535898


const int maxn = 1000010;

using namespace std;

int f[maxn];
int p[maxn];
int n, m;

LL Count(int x)
{
    LL cnt = 0;
    for(int i = 0; i < n; i++)
        if(f[i] < x)
            cnt += x-f[i];
    for(int i = 0; i < m; i++)
        if(p[i] > x)
            cnt += p[i]-x;
    return cnt; 
}

int main()
{
    while(cin >>n>>m)
    {
        for(int i = 0; i < n; i++)
            cin >>f[i];
        for(int i = 0; i < m; i++)
            cin >>p[i];
        LL sum = 1e18;
        LL cnt, cnt1;
        double l = 1;
        double r = 1e9;
        while(l < r-eps)
        {
            double lmid = l+(r-l)/3;
            double rmid = r-(r-l)/3;
            cnt = Count(floor(lmid));// 返回小於或者等於指定表示式的最大整數
            cnt1 = Count(ceil(rmid));//返回大於或者等於指定表示式的最小整數
            if(cnt < cnt1)
                r = rmid;
            else
                l = lmid;
            sum = min(cnt, min(sum, cnt1));
        }
        cout<<sum<<endl;
    }
    return 0;
}


相關文章