codeforces(567D)--D. One-Dimensional Battle Ships

D. One-Dimensional Battle Ships
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Sample test(s)
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

题目大意:有一个1*n的长方形由n个1*1的小块组成,Alice在上面放了k个1*a的小长方形代表船,船不能相互重叠和相邻,现在Bob每次询问第i个小块那是不是有船,但是Alice都答不是,现在让你判断一下当第几次询问后,就可以判断出Alice在撒谎,如果都不能判断出,那么输出-1

对于每一次询问都相当于将一段连续的区间截成了两个小区间 ,在区间中可以计算区间的程度和区间最多可以放下多少1*a的小正方形,当没有询问时,可以放的是最多的,每当一次询问后,区间变小,都会有可能使能放的个数减少,找到最初使总数小于k的那次询问并输出。

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std ;
#define LL __int64
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
LL cl1[800010] , cl2[800010] ;
LL s[200010] ;
void push_up1(LL rt) {
    cl1[rt] = max(cl1[rt<<1],cl1[rt<<1|1]) ;
}
void push_up2(LL rt) {
    cl2[rt] = min(cl2[rt<<1],cl2[rt<<1|1]) ;
}
void update(LL k,LL l,LL r,LL rt) {
    if( l == r ) {
        cl1[rt] = cl2[rt] = l ;
        return ;
    }
    if( k <= (l+r)/2 )
        update(k,l,(l+r)/2,rt<<1) ;
    else
        update(k,(l+r)/2+1,r,rt<<1|1) ;
    push_up1(rt) ;
    push_up2(rt) ;
}
LL query1(LL ll,LL rr,LL l,LL r,LL rt) {
    if( ll > r || rr < l ) return -1 ;
    if( ll <= l && rr >= r ) return cl1[rt] ;
    LL t1 = query1(ll,rr,l,(l+r)/2,rt<<1) , t2 = query1(ll,rr,(l+r)/2+1,r,rt<<1|1) ;
    if( t1 >= t2 ) return t1 ;
    return t2 ;
}
LL query2(LL ll,LL rr,LL l,LL r,LL rt) {
    if( ll > r || rr < l ) return INF ;
    if( ll <= l && rr >= r ) return cl2[rt] ;
    LL t1 = query2(ll,rr,l,(l+r)/2,rt<<1) , t2 = query2(ll,rr,(l+r)/2+1,r,rt<<1|1) ;
    if( t1 <= t2 ) return t1 ;
    return t2 ;
}
void create(LL l,LL r,LL rt) {
    cl1[rt] = -1 ;
    cl2[rt] = INF ;
    if( l == r ) {
        return ;
    }
    create(l,(l+r)/2,rt<<1) ;
    create((l+r)/2+1,r,rt<<1|1) ;
}
int main() {
    LL n , k , a , m , i , j , num , l , r , len ;
    scanf("%I64d %I64d %I64d", &n, &k, &a) ;
    create(0,n+1,1) ;
    scanf("%I64d", &m) ;
    for(i = 1 ; i <= m ; i++)
        scanf("%I64d", &s[i]) ;
    if( a*k+k-1 > n ) {
        printf("0\n") ;
        return 0;
    }
    update(0,0,n+1,1) ;
    update(n+1,0,n+1,1) ;
    num = (n+1)/(a+1) ;
    for(i = 1 ; i <= n ; i++) {
        l = query1(0,s[i],0,n+1,1) ;
        r = query2(s[i],n+1,0,n+1,1) ;
        if( l == -1 ) l = 0 ;
        if( r == INF ) r = n+1 ;
        len = r - l - 1 ;
        if( len < a ) continue ;
        num -= (len+1)/(a+1) ;
        num += ( s[i]-l )/(a+1) ;
        num += (r-s[i])/(a+1) ;
        if( num < k ) break ;
        update(s[i],0,n+1,1) ;
    }
    if( i <= n )
        printf("%I64d\n", i) ;
    else
        printf("-1\n") ;
    return 0 ;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值