2015. A New Year Gift
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
Windbreaker was planning to send his friends some necklaces as New Year gifts. To show sincerity ,he decided to make the necklaces all by himself. He bought some kinds of pearls and each kind of pearls has a different color from others. He wanted to make each necklace consisted of M pearls from different kinds, that’s what he called M-perfect necklace. Windbreaker wanted to know the maximum number of necklaces he could send out to his friends. The number of pearls of each kind and M are given, and now you are asked to tell Windbreaker how many M-perfect necklaces he could make at most
Input
You will be given a number of cases in the input. Each case starts with a positive integer n(n<=1000),which is the number of different kinds;
The second line contains n positive numbers represent for the number of pearls of each kind which will not exceed 2000.
The third line contains a positive number M(1<=M<=100).
The end of input is indicated by a line containing n=0.
Output
For each test case, output a line containing the Maximum number of M-perfect necklaces.
Sample Input
5 3 3 3 3 3 5 6 1 2 3 4 5 6 5 0
Sample Output
3 3
Problem Source
每周一赛:2010中山大学新手赛
// Problem#: 2015
// Submission#: 3370598
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <string.h>
using namespace std;
char text[10000];
int main() {
//std::ios::sync_with_stdio(false);
int N, M, num[1005], ans;
while (scanf("%d", &N) && N) {
gets(text);
gets(text);
int cal = 0;
int p = 0;
for (int i = 0; text[i] != '\0'; i++) {
if (text[i] == ' ') {
num[p++] = cal;
cal = 0;
} else {
cal = cal * 10 + text[i] - '0';
}
}
num[p] = cal;
int S = 0;
for (int i = 0; i < N; i++) S += num[i];
scanf("%d", &M);
int L = 0, B = S;
while (L <= B) {
int mid = (L + B) / 2;
int sum = 0;
for (int i = 0; i < N; i++) sum += min(mid, num[i]);
if (M * mid > sum) B = mid - 1;
else {
L = mid + 1;
ans = mid;
}
}
printf("%d\n", ans);
}
//cin >> N;
return 0;
}