您的位置:首页 > 其它

C. Anya and Ghosts(Codeforces Round #288 (Div. 2))

2015-01-28 21:08 766 查看
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
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: OK

Input
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'


Test: #16, time: 15 ms., memory: 40 KB, exit code: 0, checker exit code: 0, verdict: OK

Input
5 3 31 4 5 6 10


Output
11


Answer
11


Checker Log
ok answer is '11'


Test: #17, time: 0 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK

Input
9 7 12 3 4 5 6 7 8 9 10


Output
2


Answer
2


Checker Log
ok answer is '2'


Test: #18, time: 0 ms., memory: 32 KB, exit code: 0, checker exit code: 0, verdict: OK

Input
8 18 32 3 4 5 6 7 8 9


Output
3


Answer
3


Checker Log
ok answer is '3'


Test: #19, time: 15 ms., memory: 40 KB, exit code: 0, checker exit code: 0, verdict: OK

Input
88 82 3616 17 36 40 49 52 57 59 64 66 79 80 81 82 87 91 94 99 103 104 105 112 115 117 119 122 123 128 129 134 135 140 146 148 150 159 162 163 164 165 166 168 171 175 177 179 181 190 192 194 196 197 198 202 203 209 211 215 216 223 224 226 227 228 230 231 232 234 235 242 245 257 260 262 263 266 271 274 277 278 280 281 282 284 287 290 296 297


Output
144


Answer
144


Checker Log
ok answer is '144'


Test: #20, time: 15 ms., memory: 36 KB, exit code: 0, checker exit code: 0, verdict: OK

Input
160 203 100
5 6 7 8 11 13 15 16 17 20 22 24 25 26 28 29 30 32 40 41 45 48 49 55 58 60 62 64 65 66 67 69 70 72 73 74 75 76 80 81 84 87 88 90 91 92 94 95 96 97 98 100 105 108 110 112 113 116 117 119 121 122 123 124 128 129 130 132 133 134 135 136 140 141 144 145 146 149 150 151 152 154 156 159 160 167 168 169 170 171 172 173 174 179 180 182 183 185 186 187 188 189 190 191 192 199 200 201 202 204 205 206 209 210 214 215 220 221 223 224 226 227 228 229 230 238 239 243 244 247 249 250 253 254 255 257 258 260 2...


Output
200


Answer
200


Checker Log
ok answer is '200'


Test: #21, time: 15 ms., memory: 32 KB, exit code: 0, checker exit code: 0, verdict: OK

Input
131 205 231 3 8 9 10 11 12 13 14 17 18 19 23 25 26 27 31 32 33 36 37 39 40 41 43 44 51 58 61 65 68 69 71 72 73 75 79 80 82 87 88 89 90 91 92 93 96 99 100 103 107 109 113 114 119 121 122 123 124 127 135 136 137 139 141 142 143 144 148 149 151 152 153 154 155 157 160 162 168 169 170 171 172 174 176 177 179 182 183 185 186 187 190 193 194 196 197 200 206 209 215 220 224 226 230 232 233 235 237 240 242 243 244 247 251 252 260 264 265 269 272 278 279 280 281 288 290 292 294 296 300


Output
46


Answer
46


Checker Log
ok answer is '46'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: