19.1.23 CJK Round 1A 2015
Problem A. Mushroom Monster
Problem
Kaylin loves mushrooms. Put them on her plate and she‘ll eat them up! In this problem she‘s eating a plate of mushrooms, and Bartholomew is putting more pieces on her plate.
In this problem, we‘ll look at how many pieces of mushroom are on her plate at 10-second intervals. Bartholomew could put any non-negative integer number of mushroom pieces down at any time, and the only way they can leave the plate is by being eaten.
Figure out the minimum number of mushrooms that Kaylin could have eaten using two different methods of computation:
- Assume Kaylin could eat any number of mushroom pieces at any time.
- Assume that, starting with the first time we look at the plate, Kaylin eats mushrooms at a constant rate whenever there are mushrooms on her plate.
For example, if the input is 10 5 15 5
:
With the first method, Kaylin must have eaten at least 15 mushroom pieces: first she eats 5, then 10 more are put on her plate, then she eats another 10. There‘s no way she could have eaten fewer pieces.
With the second method, Kaylin must have eaten at least 25 mushroom pieces. We can determine that she must eat mushrooms at a rate of at least 1 piece per second. She starts with 10 pieces on her plate. In the first 10 seconds, she eats 10 pieces, and 5 more are put on her plate. In the next 5 seconds, she eats 5 pieces, then her plate stays empty for 5 seconds, and then Bartholomew puts 15 more pieces on her plate. Then she eats 10 pieces in the last 10 seconds.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each will consist of one line containing a single integer N, followed by a line containing N space-separated integers mi; the number of mushrooms on Kaylin‘s plate at the start, and at 10-second intervals.
Output
For each test case, output one line containing "Case #x: y z", where x is the test case number (starting from 1), y is the minimum number of mushrooms Kaylin could have eaten using the first method of computation, and z is the minimum number of mushrooms Kaylin could have eaten using the second method of computation.
Limits
1 ≤ T ≤ 100.
Small dataset
2 ≤ N ≤ 10.
0 ≤ mi ≤ 100.
Large dataset
2 ≤ N ≤ 1000.
0 ≤ mi ≤ 10000.
Sample
Input |
Output |
4
4
10 5 15 5
2
100 100
8
81 81 81 81 81 81 81 0
6
23 90 40 0 100 9
|
Case #1: 15 25
Case #2: 0 0
Case #3: 81 567
Case #4: 181 244
|
1 #include <iostream> 2 #include <fstream> 3 #include <stdio.h> 4 #include <algorithm> 5 #include <string> 6 #include <string.h> 7 #include <vector> 8 #include <set> 9 #include <queue> 10 #define inf 9999999 11 using namespace std; 12 13 int rd; 14 int check[1005]; 15 ifstream infile; 16 ofstream outfile; 17 18 void init(int kase) { 19 infile >> rd; 20 int ans1 = 0, maxgap = 0; 21 for (int i = 1; i <= rd; i++) { 22 infile >> check[i]; 23 int gap; 24 if (check[i] < check[i - 1]) { 25 gap = check[i - 1] - check[i]; 26 ans1 += gap; 27 maxgap = max(gap, maxgap); 28 } 29 } 30 int ans2 = 0; 31 for (int i = 1; i <= rd - 1; i++) 32 ans2 += min(maxgap, check[i]); 33 cout << "Case #" << kase << ": " << ans1 << " " << ans2 << endl; 34 outfile << "Case #" << kase << ": " << ans1 << " " << ans2 << endl; 35 } 36 37 int main() 38 { 39 infile.open("A-large-practice.in"); 40 outfile.open("A-large-practice.out"); 41 int kase; 42 infile >> kase; 43 for (int i = 1; i <= kase; i++) 44 init(i); 45 }View Code
Problem B. Haircut
Problem
You are waiting in a long line to get a haircut at a trendy barber shop. The shop has Bbarbers on duty, and they are numbered 1 through B. It always takes the kth barber exactly Mk minutes to cut a customer‘s hair, and a barber can only cut one customer‘s hair at a time. Once a barber finishes cutting hair, he is immediately free to help another customer.
While the shop is open, the customer at the head of the queue always goes to the lowest-numbered barber who is available. When no barber is available, that customer waits until at least one becomes available.
You are the Nth person in line, and the shop has just opened. Which barber will cut your hair?
Input
The first line of the input gives the number of test cases, T. T test cases follow; each consists of two lines. The first contains two space-separated integers B and N -- the number of barbers and your place in line. The customer at the head of the line is number 1, the next one is number 2, and so on. The second line contains M1, M2, ..., MB.
Output
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the number of the barber who will cut your hair.
Limits
1 ≤ T ≤ 100.
1 ≤ N ≤ 109.
Small dataset
1 ≤ B ≤ 5.
1 ≤ Mk ≤ 25.
Large dataset
1 ≤ B ≤ 1000.
1 ≤ Mk ≤ 100000.
Sample
Input |
Output |
3
2 4
10 5
3 12
7 7 7
3 8
4 2 1
|
Case #1: 1
Case #2: 3
Case #3: 1
|
In Case #1, you are the fourth person in line, and barbers 1 and 2 take 10 and 5 minutes, respectively, to cut hair. When the shop opens, the first customer immediately has the choice of barbers 1 and 2, and she will choose the lowest-numbered barber, 1. The second customer will immediately be served by barber 2. The third customer will wait since there are no more free barbers. After 5 minutes, barber 2 will finish cutting the second customer‘s hair, and will servethe third customer. After 10 minutes, both barbers 1 and 2 will finish; you are next in line, and you will have the choice of barbers 1 and 2, and will choose 1.
題解:
這道題讓我們知道了這種形式的比賽是如何卡時間的……
我一開始想的方法是找全一個周期中不同排名的顧客分配到的理發師編號,但還是太慢,沒法過大數據集,只能過小的:
1 #include <iostream> 2 #include <fstream> 3 #include <stdio.h> 4 #include <algorithm> 5 #include <string> 6 #include <string.h> 7 #include <vector> 8 #include <set> 9 #include <queue> 10 #define inf 9999999 11 using namespace std; 12 13 int n, b; 14 struct Barber { 15 int idx; 16 long long t; 17 Barber() {} 18 Barber(int id, long long tt) :idx(id), t(tt) {} 19 }; 20 ifstream infile; 21 ofstream outfile; 22 int barber[1005]; 23 24 bool operator<(Barber a, Barber b) { 25 return a.idx < b.idx; 26 } 27 28 class cmp { 29 public: 30 bool operator()(Barber a, Barber b) { 31 if (a.t == b.t)return a.idx < b.idx; 32 return a.t < b.t; 33 } 34 }; 35 36 bool judge(set<Barber, cmp>worklist) { 37 if (worklist.size() != b)return false; 38 auto p = worklist.begin(); 39 auto q = worklist.rbegin(); 40 if ((*p).t == (*q).t)return true; 41 return false; 42 } 43 44 void init(int kase) { 45 set<Barber>waitlist; 46 set<Barber, cmp>worklist; 47 vector<int>loop; 48 infile >> b >> n; 49 for (int i = 1; i <= b; i++) { 50 int x; 51 infile >> x; 52 barber[i] = x; 53 waitlist.insert(Barber(i, x)); 54 } 55 long long time = 0; 56 Barber chosen; 57 int i; 58 for (i = 1; i <= n; i++) { 59 if (judge(worklist))break; 60 if (!waitlist.empty()) { 61 chosen = *(waitlist.begin()); 62 waitlist.erase(waitlist.begin()); 63 chosen.t = time + barber[chosen.idx]; 64 worklist.insert(Barber(chosen.idx, chosen.t)); 65 loop.push_back(chosen.idx); 66 continue; 67 } 68 chosen = *(worklist.begin()); 69 time = chosen.t; 70 chosen.t = time + barber[chosen.idx]; 71 worklist.erase(worklist.begin()); 72 worklist.insert(Barber(chosen.idx, chosen.t)); 73 loop.push_back(chosen.idx); 74 } 75 cout << "Case #" << kase << ": " << loop[(n - 1) % loop.size()] << endl; 76 outfile << "Case #" << kase << ": " << loop[(n - 1) % loop.size()] << endl; 77 } 78 79 int main() 80 { 81 infile.open("B-large-practice.in"); 82 outfile.open("B-large-practice.out"); 83 int kase; 84 infile >> kase; 85 for (int i = 1; i <= kase; i++) 86 init(i); 87 }View Code
於是偷偷翻看了答案,又是二分!我永遠都不會記得用二分
1 #include <iostream> 2 #include <fstream> 3 #include <stdio.h> 4 #include <algorithm> 5 #include <string> 6 #include <string.h> 7 #include <vector> 8 #include <set> 9 #include <queue> 10 #define inf 9999999 11 using namespace std; 12 13 int n, b; 14 ifstream infile; 15 ofstream outfile; 16 int barber[1005]; 17 18 long long served(long long time) { 19 long long cstmers = 0; 20 for (int i = 1; i <= b; i++) 21 cstmers += time / barber[i] + 1; 22 return cstmers; 23 } 24 25 void init(int kase) { 26 infile >> b >> n; 27 for (int i = 1; i <= b; i++) 28 infile >> barber[i]; 29 long long s = 0, e = 10000LL * n; 30 while (s + 1 < e) { 31 long long mid = (s + e) / 2; 32 long long serve = served(mid - 1); 33 if (serve < n) 34 s = mid; 35 else 36 e = mid - 1; 37 } 38 if (served(e - 1) < n) 39 s = e; 40 long long tobeserved = n - served(s - 1); 41 for (int i = 1; i <= b; i++) 42 if (s % barber[i] == 0) { 43 tobeserved--; 44 if (tobeserved == 0) 45 { 46 cout << "Case #" << kase << ": " << i << endl; 47 outfile << "Case #" << kase << ": " << i << endl; 48 return; 49 } 50 } 51 } 52 53 int main() 54 { 55 infile.open("B-large-practice.in"); 56 outfile.open("B-large-practice.out"); 57 int kase; 58 infile >> kase; 59 for (int i = 1; i <= kase; i++) 60 init(i); 61 }View Code
因為long long的原因WA了好久……真的好久……佛了
19.1.23 CJK Round 1A 2015