【链接】 我是链接,点我呀:)

【题意】

长度为n的一个序列,其中有一些部分可能是空的,一些部分是长度为a的物品的一部分

(总共有k个长度为a的物品,一个放在位置i长度为a的物品会占据i,i+1,....i+a-1这a个格子

(物品之间必须要有至少一个空格,不能相邻

现在有一个人猜了m个不同的位置

你每次都说那个位置不是物品的一部分。(骗他)

那么请问最少在第几次你会露馅?(知道你在骗他)

【题解】

对于一个连续的空白的区间[l,r],设其长度为len=r-l+1
那么这个空白区间最多能放下(len+1)/(a+1)个物品
(注意每个物品之间至少要有一个空格,所以每个物品理论上占据了a+1个空间
(但是最后一个物品它最后面可以不用有空格,因此我们可以多给它一个"虚拟"空间,覆盖到这种情况
可以这样做,我们按顺序加入那个人猜的断点
将包含这个断点的区间(一开始只有[1,n+1),注意一定要用左闭右开区间,不然类似[x,x]这样的区间不好表示
分成[l,x-1]和[x+1,r]
并且减去[l,r]能放下的物品数
然后改为增加两个子区间能放下的物品数
直到能放下的物品数量小于k为止
(用一个一个的点来表示区间,注意这个区间里面的区间都是左闭右开区间)
(在分成两个子区间之后,我们会在集合中加入x和x+1,理论上我们还能通过查找x找到[x,x+1)这个区间,但是这个区间其实是不存在的了)
(插入的x仅仅是为了给[l,x)这个区间服务的,因为不会重复猜测所以没影响

【代码】

import java.io.*;
import java.util.*; public class Main { static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static int N = 50000;
static class Task{
int n,k,a;
int m;
TreeSet myset = new TreeSet(); int _get(int len) {
return (len+1)/(a+1);
} public void solve(InputReader in,PrintWriter out) {
n = in.nextInt();k = in.nextInt();a = in.nextInt();
m = in.nextInt();
myset.add(1);myset.add(n+1);
int ans = _get(n);
for (int i = 1; i <= m;i++) {
int x;
x = in.nextInt();
int r= (int)myset.higher(x);
int l = (int)myset.floor(x);
ans = ans-_get(r-l);
//l..x-1
if (l<=x-1) {
myset.add(x);
ans = ans + _get(x-1-l+1);
} //x+1..r-1
if (x+1<=r-1) {
myset.add(x+1);
ans = ans + _get(r-1-(x+1)+1);
}
if (ans<k) {
out.println(i);
return;
}
}
out.println(-1);
}
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

【Codeforces 567D】One-Dimensional Battle Ships的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群【最大点覆盖】

    二分图最大点覆盖模型,因为对于一个点(x,y)显然只要选x或者y就好了,于是连边,跑最大匹配=最大点覆盖(不会证) #include<iostream> #include<cstdi ...

  2. [Swift通天遁地]一、超级工具-(9)在地图视图MKMapView中添加支持交互动作的标注图标

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. echarts-gl 3D柱状图保存为图片,打印

    echarts-gl生成的立体柱状图生成图片是平面的,但是需求是3D图并且可以打印,我们的思路是先转成图片,然后再打印,代码如下: 生成3D图 <td>图表分析</td> &l ...

  4. Joseph UVA 1452 Jump

    题目传送门 /* 数学:约瑟夫环问题的变形,首先定义f[i]表示剩下i个人时,最后一个选出的人,有个公式:f[i] = (f[i-1] + m) % i f[1] = 0(编号从0开始),那么类似最后 ...

  5. HTML基础2——综合案例2——复杂的嵌套列表

    <html> <head> <title></title> </head> <body> <ul type="d ...

  6. LN : leetcode 513 Find Bottom Left Tree Value

    lc 513 Find Bottom Left Tree Value 513 Find Bottom Left Tree Value Given a binary tree, find the lef ...

  7. IDEA提示 found duplicate code

    原因: IntelliJ IDEA提示Found duplicated code in this file 这不是我们代码错误,而是idea提示说我们的代码有重复,在项目的其他地方有同样的代码片段 解 ...

  8. Angular——自定义服务

    基本介绍 之前我们介绍了angular内置的几种服务,这里我们介绍如何自己定义自己的服务,主要是通过三个方法:factory.service.value 基本使用 factory:可以返回对象,也可以 ...

  9. mac系统,鼠标移动太慢

    to check your speed: defaults read -g com.apple.mouse.scaling to set your speed defaults write -g co ...

  10. 梦想CAD控件安卓参数绘图

    在CAD绘图中,参数化绘图可以帮助我们极大缩短设计时间,用户可以按照设计意图控制绘图对象,这样即使对象发生了变化,具体的关系和测量数据仍将保持不变,能够对几何图形和标注进行控制,可以帮助用户应对耗时的 ...