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

【题意】

长度为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. [Qt Quick入门] 基本元素初体验

    Qt Quick作为QML语言的标准库,提供了很多基本元素和控件来帮助我们构建Qt Quick应用.这节我们简要地介绍一些Qt Quick元素,如Rectangle.Item.Text.Button. ...

  2. 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

    题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...

  3. Ant安装以及环境配置以及使用[windows环境]

    一.安装ant 官方主页http://ant.apache.org下载新版的ant. *下载对应的版本,解压到我们的硬盘. 二.配置环境变量 Window中设置ant环境变量: ANT_HOME    ...

  4. ASP.NET控件之RadioButtonList

    “RadioButtonList”控件表示一个封装了一组单选按钮控件的列表控件. 可以使用两种类型的 ASP.NET 控件将单选按钮添加到网页上:各个“RadioButton”控件或一个“RadioB ...

  5. JavaScript(七)数组

    Array类型 1.创建数组 字面量 var arr = [];//不要在低版本的浏览其中创建字面量的时候最后 //一个item后面加 逗号 低版本会 再新建一个空的item 构造函数 var arr ...

  6. Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载

    元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...

  7. Android基础TOP6_1:FrameLyayout和ImageView制作层叠图片

    Activity: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  8. 使用脚本快速线程转储及列出高cpu线程

    jstack `ps -ef | grep java | grep bocai.jar | awk '{print $2}'` > cpu_high.logtop -b -n1 -Hp `ps ...

  9. 【转】WITH AS 用法

    转载自:http://blog.csdn.net/shaochao14/article/details/6223052 一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery ...

  10. codeforces_333B_水过

    B. Chips time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...