Codeforces 567D:One-Dimensional Battle Ships(二分)
time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : standard output
Problem Description#
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of square cells (that is, on a table).
At the beginning of the game Alice puts ships on the field without telling their positions to Bob. Each ship looks as a rectangle (that is, it occupies a sequence of consecutive squares of the field). The ships cannot intersect and even touch each other.
After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit").
But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss".
Help Bob catch Alice cheating — find Bob's first move, such that after it you can be sure that Alice cheated.
Input#
The first line of the input contains three integers: and — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the and are such that you can put ships of size on the field, so that no two ships intersect or touch each other.
The second line contains integer — the number of Bob's moves.
The third line contains distinct integers , where is the number of the cell where Bob made the -th shot. The cells are numbered from left to right from to .
Output#
Print a single integer — the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from to in the order the were made. If the sought move doesn't exist, then print "".
Examples#
input
11 3 3
5
4 8 6 1 11
output
3
input
5 1 3
2
1 5
output
-1
input
5 1 3
1
3
output
1
题意#
一个长为的区域内有个战舰,每个战舰的长度均为,Bob射击个不同的位置,对于每次射击,Alice都会告诉Bob是否击中。
但是因为Alice会撒谎,所以当Bob击中的时候,Alice也会说没有击中
问Bob最早可以在第几次射击的时候发现Alice在说谎
思路#
因为随着射击次数的增多,Bob发现Alice说谎的可能性越大,所以可以利用二分来解决
将区间的位置进行排序,然后计算这些位置如果全部没有击中,可以放下多少战舰,如果超过个,那么当前的是可行的,查询前半部分,否则,查询后半部分
代码#
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int x[maxn];
int b[maxn];
int n,k,a;
bool check(int mid)
{
for(int i=1;i<=mid;i++)
b[i]=x[i];
sort(b+1,b+1+mid);
int cnt=0;
for(int i=1;i<=mid;i++)
cnt+=(b[i]-b[i-1])/(a+1); // b[i]和b[i-1]之间可以放多少战舰
// b[mid]->最后一个位置可以放多少战舰
cnt+=(n-b[mid]+1)/(a+1);
if(cnt>=k)
return true;
return false;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("/home/wzy/in", "r", stdin);
freopen("/home/wzy/out", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k>>a;
int m;
cin>>m;
for(int i=1;i<=m;i++)
{
cin>>x[i];
}
int l=1,r=m,ans=inf;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
l=mid+1;
else
r=mid-1,ans=min(ans,mid);
}
if(ans==inf)
cout<<-1<<endl;
else
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}
作者:Friends-A
出处:https://www.cnblogs.com/Friends-A/p/11686574.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2018-10-16 NYOJ 6:喷水装置(一)(贪心)