Title Link: Http://codeforces.com/problemset/problem/567/D
Title: Give a field of 1 * n, numbered from left to right in turn 1,2,...,n. After asking the M gun (I shot number XI, Xi is not allowed to place the vessel!) ), can you put the K-only 1 * a boat into the field that has not been shot numbered. Since Alice every time shoot will say miss, that is missed, you need to determine the number of times shoot make the whole field can not place K only boat, if all can put, the final output-1.
First of all, foreigners are really good and rigorous, and good body ~ ~ ~
There are a few things to explain. Actually, it's been confusing me for a long time. 0.0
(1) At the beginning the whole range (n+1)/(a+1) may be a lot of people still do not understand. Then the column equation helps to understand. Assuming that you can place up to X-boats in [1, n], you can list X * a + (x-1) = N, so you can get x = (n+1)/(a+1) by merging similar terms. Or it can be understood as a matter of fact, because the maximum number of ships must be from the edge of the first place, and then after the interval of one shoot and then into the other ship ... It is easy to know that each ship occupies at least a+1 (the last one may be more or a (gratifying)), then we can virtual a number of n+1 field. So the whole length is (n+1)/(a+1), very magical Wow ~
(2) (r-l+2)/(a+1) What is it?! If you understand the 1th, it is easy to understand. After we shoot, this shoot divides the space into divided, the selected space we use the set set of the Upper_bound to find, the return is R, the former is L. R-l+1 is easy to understand (don't understand that you can talk to me privately), and then +1 is the virtual field. Point (1) has already been said, the next to the edge placement is the optimal solution conditions! Finally, remember to put the shoot number in the next round of alternate space.
(3) Why the space should be reduced and added. Because there is a shoot space in two ~ ~ This time can be placed in the ship is not the original [L, R] so much. It is divided by the left interval [L, x-1] and [X+1, R] and so much more.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5#include <Set>6 using namespacestd;7 8 Set<int>s;9 Ten intMain () One { A #ifndef Online_judge -Freopen ("In.txt","R", stdin); - #endif //Online_judge the - intm, N, a, k, X; - while(SCANF ("%d%d%d", &n, &k, &a)! =EOF) { -scanf"%d", &m); + s.clear (); -S.insert (0), S.insert (n+1);//tips for implementing virtual lengths + intsum = (n+1)/(A +1); A intAns =-1, F =0; at for(inti =1; I <= m; i++) { -scanf"%d", &x); - Set<int>::iterator it =s.upper_bound (x); - intr = *it; - intL = * (--it); -sum = Sum-(r-l)/(A +1) + (X-L)/(A +1) + (R-x)/(A +1); in - if(Sum < K &&!)f) { toAns =i; +f =1; - } the S.insert (x); * } $printf"%d\n", ans);Panax Notoginseng } - return 0; the}
Codeforces 567D. One-dimensional Battle Ships problem solving report