C. Anya and Ghosts( Codeforces Round #288 (Div. 2))
2015-01-29 08:12
344 查看
C. Anya and Ghosts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits,
each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light
one candle, then this candle burns for exactly t seconds and then goes out and can no longer be used.
For each of the m ghosts Anya knows the time at which it comes: the i-th
visit will happen wi seconds
after midnight, all wi's
are distinct. Each visit lasts exactly one second.
What is the minimum number of candles Anya should use so that during each visit, at least r candles are burning? Anya can start to light a candle at any
time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of
seconds after a midnight, or in other words in any integer moment of time.
Input
The first line contains three integers m, t, r (1 ≤ m, t, r ≤ 300),
representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit.
The next line contains m space-separated numbers wi (1 ≤ i ≤ m, 1 ≤ wi ≤ 300),
the i-th of them repesents at what second after the midnight the i-th
ghost will come. All wi's
are distinct, they follow in the strictly increasing order.
Output
If it is possible to make at least r candles burn during each visit, then print the minimum number of candles that Anya needs to light for that.
If that is impossible, print - 1.
Sample test(s)
input
output
input
output
input
output
Note
Anya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.
It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.
In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th
and 7-th seconds after the midnight.
In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.
In the third sample test the answer is - 1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment
when the ghost comes.
Test: #1, time: 15 ms., memory: 40 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #2, time: 15 ms., memory: 40 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #3, time: 15 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #4, time: 0 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #5, time: 0 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #6, time: 15 ms., memory: 32 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #7, time: 15 ms., memory: 40 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #8, time: 15 ms., memory: 32 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #9, time: 15 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #10, time: 15 ms., memory: 28 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #11, time: 31 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #12, time: 30 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #13, time: 0 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #14, time: 0 ms., memory: 32 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
Test: #15, time: 31 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
Output
Answer
Checker Log
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits,
each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light
one candle, then this candle burns for exactly t seconds and then goes out and can no longer be used.
For each of the m ghosts Anya knows the time at which it comes: the i-th
visit will happen wi seconds
after midnight, all wi's
are distinct. Each visit lasts exactly one second.
What is the minimum number of candles Anya should use so that during each visit, at least r candles are burning? Anya can start to light a candle at any
time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of
seconds after a midnight, or in other words in any integer moment of time.
Input
The first line contains three integers m, t, r (1 ≤ m, t, r ≤ 300),
representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit.
The next line contains m space-separated numbers wi (1 ≤ i ≤ m, 1 ≤ wi ≤ 300),
the i-th of them repesents at what second after the midnight the i-th
ghost will come. All wi's
are distinct, they follow in the strictly increasing order.
Output
If it is possible to make at least r candles burn during each visit, then print the minimum number of candles that Anya needs to light for that.
If that is impossible, print - 1.
Sample test(s)
input
1 8 3 10
output
3
input
2 10 1 5 8
output
1
input
1 1 310
output
-1
Note
Anya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.
It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.
In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th
and 7-th seconds after the midnight.
In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.
In the third sample test the answer is - 1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment
when the ghost comes.
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; struct node { int x; int y; }q[3010]; int a[3001]; int n,m,k; int main() { while(scanf("%d%d%d",&n,&m,&k)!=EOF) { memset(a,0,sizeof(a)); memset(q,0,sizeof(q)); for(int i=0;i<n;i++) { scanf("%d",&a[i]); } if(m<k) { printf("-1\n"); continue; } int kk = m; int sum = 0; int flag = 0; for(int i=0;i<n;i++) { if(i == 0) { sum = k; for(int j=a[i]-k;j<a[i];j++) { q[j].y = 1; } for(int j=a[i];j<a[i]+m;j++) { q[j].x = kk; kk--; } } else { if(q[a[i]].x < k) { //printf("q[%d].x = %d\n",a[i],q[a[i]].x); int cnt = k - q[a[i]].x; sum += cnt; for(int j=a[i]-cnt;j<a[i];j++) { if(q[j].y == 1) { flag = 1; break; } q[j].y = 1; for(int v=j+1;v<m+j+1;v++) { q[v].x++; } } } } if(flag == 1) { break; } } if(flag == 1) { printf("-1\n"); } else { printf("%d\n",sum); } } return 0; }
→Judgement Protocol
Test: #1, time: 15 ms., memory: 40 KB, exit code: 0, checker exit code: 0, verdict: OKInput
1 8 3 10
Output
3
Answer
3
Checker Log
ok answer is '3'
Test: #2, time: 15 ms., memory: 40 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
2 10 1 5 8
Output
1
Answer
1
Checker Log
ok answer is '1'
Test: #3, time: 15 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1 1 310
Output
-1
Answer
-1
Checker Log
ok answer is '-1'
Test: #4, time: 0 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
21 79 113 42 51 60 69 77 94 103 144 189 196 203 210 215 217 222 224 234 240 260 282
Output
4
Answer
4
Checker Log
ok answer is '4'
Test: #5, time: 0 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
125 92 21 2 3 4 5 7 8 9 10 12 17 18 20 21 22 23 24 25 26 28 30 32 33 34 35 36 37 40 41 42 43 44 45 46 50 51 53 54 55 57 60 61 62 63 69 70 74 75 77 79 80 81 82 83 84 85 86 88 89 90 95 96 98 99 101 103 105 106 107 108 109 110 111 112 113 114 118 119 120 121 122 123 124 126 127 128 129 130 133 134 135 137 139 141 143 145 146 147 148 149 150 151 155 157 161 162 163 165 166 167 172 173 174 176 177 179 181 183 184 185 187 188 189 191 194
Output
6
Answer
6
Checker Log
ok answer is '6'
Test: #6, time: 15 ms., memory: 32 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
42 100 2 55 56 57 58 60 61 63 66 71 73 75 76 77 79 82 86 87 91 93 96 97 98 99 100 101 103 108 109 111 113 114 117 119 122 128 129 134 135 137 141 142 149
Output
2
Answer
2
Checker Log
ok answer is '2'
Test: #7, time: 15 ms., memory: 40 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
31 23 242 43 44 47 48 49 50 51 52 56 57 59 60 61 64 106 108 109 110 111 114 115 116 117 118 119 120 123 126 127 128
Output
6
Answer
6
Checker Log
ok answer is '6'
Test: #8, time: 15 ms., memory: 32 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
300 300 80 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152...
Output
159
Answer
159
Checker Log
ok answer is '159'
Test: #9, time: 15 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
9 12 41 2 3 4 5 7 8 9 10
Output
5
Answer
5
Checker Log
ok answer is '5'
Test: #10, time: 15 ms., memory: 28 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
9 16 21 2 3 4 6 7 8 9 10
Output
2
Answer
2
Checker Log
ok answer is '2'
Test: #11, time: 31 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
7 17 31 3 4 5 7 9 10
Output
3
Answer
3
Checker Log
ok answer is '3'
Test: #12, time: 30 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1 1 14
Output
1
Answer
1
Checker Log
ok answer is '1'
Test: #13, time: 0 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
9 1 31 2 4 5 6 7 8 9 10
Output
-1
Answer
-1
Checker Log
ok answer is '-1'
Test: #14, time: 0 ms., memory: 32 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
9 10 41 2 3 4 5 6 8 9 10
Output
7
Answer
7
Checker Log
ok answer is '7'
Test: #15, time: 31 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
7 2 21 2 3 4 6 7 9
Output
10
Answer
10
Checker Log
ok answer is '10'
相关文章推荐
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts 模拟 贪心
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts
- C. Anya and Ghosts(Codeforces Round #288 (Div. 2))
- Codeforces Round #288 (Div. 2)C. Anya and Ghosts(模拟+贪心)
- Codeforces Round #288 (Div. 2)C. Anya and Ghosts
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts 模拟
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts 贪心
- Codeforces Round #288 (Div. 2)---C. Anya and Ghosts
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts(贪心)
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts
- C. Anya and Ghosts(Codeforces Round #288(div2)
- Codeforces Round #293 (Div. 2) C.Anya and Smartphone(模拟)
- codeforces--C - Anya and Ghosts(贪心+模拟)
- Codeforces Round #293 (Div. 2)——C——Anya and Smartphone
- codeforces--C - Anya and Ghosts(贪心)
- Codeforces 508C Anya and Ghosts【贪心】
- Codeforces 508C Anya and Ghosts