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.
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.
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.
1 8 3 10
3
2 10 1 5 8
1
1 1 3 10
-1
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
1 8 3 10
3
3
ok answer is '3'
2 10 1 5 8
1
1
ok answer is '1'
1 1 3 10
-1
-1
ok answer is '-1'
21 79 1 13 42 51 60 69 77 94 103 144 189 196 203 210 215 217 222 224 234 240 260 282
4
4
ok answer is '4'
125 92 2 1 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
6
6
ok answer is '6'
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
2
2
ok answer is '2'
31 23 2 42 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
6
6
ok answer is '6'
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...
159
159
ok answer is '159'
9 12 4 1 2 3 4 5 7 8 9 10
5
5
ok answer is '5'
9 16 2 1 2 3 4 6 7 8 9 10
2
2
ok answer is '2'
7 17 3 1 3 4 5 7 9 10
3
3
ok answer is '3'
1 1 1 4
1
1
ok answer is '1'
9 1 3 1 2 4 5 6 7 8 9 10
-1
-1
ok answer is '-1'
9 10 4 1 2 3 4 5 6 8 9 10
7
7
ok answer is '7'
7 2 2 1 2 3 4 6 7 9
10
10
ok answer is '10'