Day Five(贪心)

来源:互联网 发布:易语言可以解析json吗? 编辑:程序博客网 时间:2024/04/28 04:16

Devu, the Singer and Churu, the Joker

CodeForces 439A http://codeforces.com/problemset/problem/439/A

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 n, d (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.

Examples
input
3 30
2 2 1
output
5
input
3 20
2 1 1
output
-1

题意是配合着百度翻译阅读的。Devu是歌手,比较大牌,Churu是讲笑话的小丑。在一个活动中,活动时长为d分钟, Devu准备了n首歌,每一首歌演唱需要ai的时间,每次演唱完至少休息十分钟才能演唱下一首;Churu五分钟可以讲一个笑话,并且可以不休息连续表演。求你怎样安排让Churu尽可能多的讲笑话的个数
最开始看错题了,以为是要求安排演出有多少种方式。这道题很简单,思路就是先判断唱歌+休息的时间够不够表演,如果>d,则直接安排不了;否则,则可以安排。Churu可以讲笑话的个数max=歌手休息的次数*(10min/5min)+除去歌手表演的剩余时间/5min

#include<iostream>using namespace std;int a[100];int main(){    int n,d,i,sum;    cin>>n>>d;              //演唱歌曲的数量和总的表演时间    for(i = 0;i < n;i++){        cin>>a[i];        d -= a[i];    }    sum = d-(n-1)*10;    if(sum<0){     //无法完成所有歌曲的表演        cout<<-1<<endl;        return 0;    }    else{        cout<<sum/5+(n-1)*2<<endl;    }    return 0;}

Devu, the Dumb Guy

CodeForces 439B http://codeforces.com/problemset/problem/439/B

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 n, x (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 Input
Input
2 3
4 1
Output
11
Input
4 2
5 1 2 1
Output
10
Input
3 3
1 1 1
Output
6

炒鸡简单的贪心策略。读题:Devu其实并不笨的。现在有n门科目,每门科目有m章,最初学习每一章需要x个小时,需要一次性将整门科目都学完,然后学第二门科目的时候每一章就只要x-1个小时,依次类推,但是规定学完一个章节时间不少于1小时,求全完全部科目的最少时间
就是先将每门科目的章节数升序排序,总是从章节数目最少的开始教,可以保证总的时间最少

//Runtime error on test 8  Please be careful that answer might not fit in 32 bit data type.//Runtime error on test 8  (1?≤n,x≤105)#include<iostream>#include<vector>#include<algorithm>using namespace std;int main(){    //int n,x,i,sum = 0;   Error    long long n,x,sum = 0;    int i;    cin>>n>>x;              //学科数和最开始每章节的学习时间    //vector<long long> a[n];   //Error    vector<int> a(n);    for(i = 0;i < n;i++){        cin>>a[i];    }    //sort(a,a+n);     sort(a.begin(),a.end());    for(i = 0;i < n;i++){        sum += a[i]*x;        if(x > 1){            x--;        }    }    cout<<sum<<endl;    return 0;}

Queue on Bus Stop

CodeForces 435A http://codeforces.com/problemset/problem/435/A

It’s that time of the year when the Russians flood their countryside summer cottages (dachas) and the bus stop has a lot of people. People rarely go to the dacha on their own, it’s usually a group, so the people stand in queue by groups.

The bus stop queue has n groups of people. The i-th group from the beginning has ai people. Every 30 minutes an empty bus arrives at the bus stop, it can carry at most m people. Naturally, the people from the first group enter the bus first. Then go the people from the second group and so on. Note that the order of groups in the queue never changes. Moreover, if some group cannot fit all of its members into the current bus, it waits for the next bus together with other groups standing after it in the queue.

Your task is to determine how many buses is needed to transport all n groups to the dacha countryside.

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100). The next line contains n integers: a1, a2, …, an (1 ≤ ai ≤ m).

Output
Print a single integer — the number of buses that is needed to transport all n groups to the dacha countryside.

Examples
input
4 3
2 3 2 1
output
3
input
3 4
1 2 1
output
1

代码如下

#include<iostream>using namespace std;int a[100];int main(){    int n,m,i,sum = 1;      cin>>n>>m;              //队列数和每辆巴士最多载客数    for(i = 0;i < n;i++){        cin>>a[i];    }    int t = m-a[0];      //t为前一辆车剩余可载客数    for(i = 1;i < n;i++){        if(a[i] > t){    //等待下一辆            sum++;            t = m-a[i];        }        else{            t -= a[i];        }    }    cout<<sum<<endl;    return 0;}

Wilbur and Array

CodeForces 596B http://codeforces.com/problemset/problem/596/B

Wilbur the pig is tinkering with arrays again. He has the array a1, a2, …, an initially consisting of n zeros. At one step, he can choose any index i and either add 1 to all elements ai, ai + 1, … , an or subtract 1 from all elements ai, ai + 1, …, an. His goal is to end up with the array b1, b2, …, bn.

Of course, Wilbur wants to achieve this goal in the minimum number of steps and asks you to compute this value.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array ai. Initially ai = 0 for every position i, so this array is not given in the input.

The second line of the input contains n integers b1, b2, …, bn ( - 109 ≤ bi ≤ 109).

Output
Print the minimum number of steps that Wilbur needs to make in order to achieve ai = bi for all i.

Examples
input
5
1 2 3 4 5
output
5
input
4
1 2 2 1
output
3

将一个数组变成另外一个数组

#include<iostream>#include<vector>#include<cstring>#include<cmath>using namespace std;int main(){    int i,j;    long long n,sum = 0,now = 0;    cin>>n;    vector<long long> a(n,0);  //vector<int> v(500, 1)    // memset(a,0,sizeof(a));  error    vector<long long> b(n);    for(i = 0;i < n;i++){        cin>>b[i];    }    /*for(i = 0;i < n;i++){        di = b[i]-a[i];        sum += abs(di);        for(j = i;j < n;j++){            a[j] = di>0? a[j]+di:(di<0?a[j]-di:a[j]);        }    }*/    for(i = 0;i < n;i++){        if(now != b[i]){            sum += abs(b[i]-now);            now = b[i];        }    }    cout<<sum<<endl;    return 0;}
0 0
原创粉丝点击