Codeforences Round #PI(div2) D. One-Dimensional Battle Ships

本文介绍了一种在一维战舰游戏中检测玩家作弊的方法。通过跟踪玩家射击位置及更新可放置船只的最大数量来判断玩家是否说谎。具体实现利用了集合数据结构,并通过查找最近的已射击位置来高效计算剩余的船只放置空间。
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 × n table).

At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 × a rectangle (that is, it occupies a sequence of a 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: n, k and a (1 ≤ n, k, a ≤ 2·105) — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other.

The second line contains integer m (1 ≤ m ≤ n) — the number of Bob's moves.

The third line contains m distinct integers x1, x2, ..., xm, where xi is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n.

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 1 to m in the order the were made. If the sought move doesn't exist, then print "-1".

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

【题意】

对于lie 的判断应该基于能放的船的个数,能放的船的个数是随着射的点数的增加而减少的,射完每个点后更新能放的船的个数,如果这个时候已经无法放下k条船了,说明lie了,如果所有都射完也没发生,那么就-1
【解题思路】
由于船与串不能相邻,除了最后一条船,每条船实际占的size 应该为a+1,那么很容易知道对于长度为l的区间,能放的船的个数为(l+1)/(a+1),这是初始能放的船的个数,为最大值,当射了点b之后,破坏的是b所在的一段最大的没有被射过点的区间的连续性,做法是找到距离b点最近的左端和右端的被射过的点,可以用set 搞,找的时upper_bound,记得初始化的时候把 0点和 n+1 点当成射过的.
.【AC代码】
#include <bits/stdc++.h>
using namespace std;
int n,k,a,m,b;
set<int>s;
set<int>::iterator it;
int solve(int x,int y)//计算射了b之后减少的房船的个数
{
    return (y-x)/(a+1)-(y-b)/(a+1)-(b-x)/(a+1);
}
int main()
{
    ios::sync_with_stdio();
    cin.tie(0);
    cin>>n>>k>>a;
    cin>>m;
    int sum = (n+1)/(a+1);//当前能放船的个数
//    cout<<sum<<endl;
    s.clear();
    s.insert(0);
    s.insert(n+1);
    int ans = -1;
    bool flag = false;
    for(int i=1; i<=m; i++)
    {
        cin>>b;
        if(flag)continue;
        it = s.upper_bound(b);
        int x = *it;
        int y = *(--it);
        sum -= solve(y,x);
        if(sum<k)
        {
            ans = i;
            flag=true;
        }
        s.insert(b);
    }
    cout<<ans<<endl;
    return 0;
}


内容概要:本文档是一份关于交换路由配置的学习笔记,系统地介绍了网络设备的远程管理、交换机与路由器的核心配置技术。内容涵盖Telnet、SSH、Console三种远程控制方式的配置方法;详细讲解了VLAN划分原理及Access、Trunk、Hybrid端口的工作机制,以及端口镜像、端口汇聚、端口隔离等交换技术;深入解析了STP、MSTP、RSTP生成树协议的作用与配置步骤;在路由部分,涵盖了IP地址配置、DHCP服务部署(接口池与全局池)、NAT转换(静态与动态)、静态路由、RIP与OSPF动态路由协议的配置,并介绍了策略路由和ACL访问控制列表的应用;最后简要说明了华为防火墙的安全区域划分与基本安全策略配置。; 适合人群:具备一定网络基础知识,从事网络工程、运维或相关技术岗位1-3年的技术人员,以及准备参加HCIA/CCNA等认证考试的学习者。; 使用场景及目标:①掌握企业网络中常见的交换与路由配置技能,提升实际操作能力;②理解VLAN、STP、OSPF、NAT、ACL等核心技术原理并能独立完成中小型网络搭建与调试;③通过命令示例熟悉华为设备CLI配置逻辑,为项目实施和故障排查提供参考。; 阅读建议:此笔记以实用配置为主,建议结合模拟器(如eNSP或Packet Tracer)动手实践每一条命令,对照拓扑理解数据流向,重点关注VLAN间通信、路由选择机制、安全策略控制等关键环节,并注意不同设备型号间的命令差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值