Codeforces 307div.2

来源:互联网 发布:自学编程怎么找工作 编辑:程序博客网 时间:2024/05/06 03:26
C. GukiZ hates Boxes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way.

In total there are n piles of boxes, arranged in a line, from left to right,i-th pile (1 ≤ i ≤ n) containingai boxes. Luckily,m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are:

  1. If i ≠ n, move from pile i to pile i + 1;
  2. If pile located at the position of student is not empty, remove one box from it.

GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum timet in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner aftert seconds, but all the boxes must be removed.

Input

The first line contains two integers n andm (1 ≤ n, m ≤ 105), the number of piles of boxes and the number of GukiZ's students.

The second line contains n integers a1, a2, ...an (0 ≤ ai ≤ 109) whereai represents the number of boxes oni-th pile. It's guaranteed that at least one pile of is non-empty.

Output

In a single line, print one number, minimum time needed to remove all the boxes in seconds.

Sample test(s)
Input
2 11 1
Output
4
Input
3 21 0 2
Output
5
Input
4 1003 4 5 4
Output
5
Note

First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second).

Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall,5 seconds.

Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.


思路:首先从最大的时间开始枚举,用二分法枚举,最大时间不会超过n+a[i]...+a[n];然后依次枚举。

代码:

#include<iostream>#include<string>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>using namespace std;int a[100005], b[100005];int n, m;bool check(long long x){for (int i = 0; i<n; i++) b[i] = a[i];int foo = 0;for (int i = n - 1; i >= 0; i--){if (b[i]){long long t = x - (i + 1); //到达某个pile剩下的时间if (t <= 0) return false; //如果没有生剩下时间,就不可能搬完while (i >= 0 && t > 0){if (t >= b[i]) t -= b[i--];//如果还剩下时间,就去前面继续搬else{b[i] -= t;t = 0;}}foo++;i++;if (foo > m) return false;}}return foo <= m;}int main(){scanf("%d%d", &n, &m);long long ans = 0;for (int i = 0; i<n; i++){scanf("%d", &a[i]);ans += a[i];}ans += n;long long L = 0, R = ans;while (L + 1 < R){long long mid = L + (R - L) / 2;if (check(mid)) R = mid;else L = mid;}if (check(L)) cout<<L<<endl;else cout<<R<<endl;return 0;}

顺便把B题贴上:

思路是:先把a,b,c串中各自字母出现的次数统计出来,因为题意是把a随意变换,求b,c,串出现次数最多。所以先求出b串中出现的字母,在a串中成倍的减去,求出倍数最小的就是b串可以在a中出现的最多次数。同样的方法再求c.

/#include <bits/stdc++.h>#include<iostream>#include<string.h>#include<algorithm>#include<stdio.h>using namespace std;int ta[30], tb[30], tc[30];int main(){    string a, b, c;    while(cin>>a>>b>>c){        memset(ta, 0, sizeof(ta));        memset(tb, 0, sizeof(tb));        memset(tc, 0, sizeof(tc));        for(int i=0; i<a.size(); i++) ta[ a[i] - 'a' ] ++;        for(int i=0; i<b.size(); i++) tb[ b[i] - 'a' ] ++;        for(int i=0; i<c.size(); i++) tc[ c[i] - 'a' ] ++;        int ans = 123456789;        for(int i=0; i<26; i++)            if(tb[i]) ans = min(ans, ta[i] / tb[i]);        int ansb = 0, ansc = 0;        for(int i=0; i<=ans; i++){            int cnt = 123456789;            for(int j=0; j<26; j++)                if(tc[j]) cnt = min(cnt, (ta[j] - tb[j] * i) / tc[j]);            if(i + cnt > ansb + ansc){                ansb = i;                ansc = cnt;            }        }        for(int i=0; i<ansb; i++) cout<<b;        for(int i=0; i<ansc; i++) cout<<c;        for(int i=0; i<26; i++)        for(int j=0; j<ta[i] - tb[i] * ansb - tc[i] * ansc; j++){            printf("%c", i + 'a');        }        puts("");    }    return 0;}


0 0
原创粉丝点击