【Codeforces 567D】One-Dimensional Battle Ships
【链接】 我是链接,点我呀:)
【题意】
长度为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的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- 【转载】7条便利的ViewState技巧
32.Seven handy ViewState tips 32.7条便利的ViewState技巧 Every time I have to deal with a classic ASP.NET W ...
- Git简介(转载)
转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137396284551 ...
- bzoj 1492: [NOI2007]货币兑换Cash【贪心+斜率优化dp+cdq】
参考:http://www.cnblogs.com/lidaxin/p/5240220.html 虽然splay会方便很多,但是懒得写,于是写了cdq 首先要想到贪心的思路,因为如果在某天买入是能得到 ...
- 微信公众号 sign类
微信公众号 Sign import java.util.UUID; import java.util.Map; import java.util.HashMap; import java.util.F ...
- mysql5.7 1055
错误提示: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggreg ...
- 理解 Java 构造函数不可以继承
参考来源:http://www.52bowen.com/a/2604620.html
- C# 生成 bmp 格式的图片
using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; usin ...
- 关于dbms_output包的使用
General Source {ORACLE_HOME}/rdbms/admin/dbmsotpt.sql First Available 7.3.4 Data Types TYPE chararr ...
- Jax
The scope of this project is to automate the current Credit Correction process of opening, editing, ...
- 简单的css缩放动画,仿腾讯新闻的分享按钮和美团app底部的图标样式
最近看到一些好看的hover的图形缩放效果.然后自己就写了下,发现这2种效果都不错.如果伙伴们更好的实现方式可以在下面留言哦~ 还有美团的效果,我就不展示了,喜欢的可以去app应用上看看. 这两种效果 ...