cf660c Select 二分 思维

Description

One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting game. He wants to take part in the game. But as we all know, you can’t get good result without teammates.
So, he needs to select two classmates as his teammates.
In this game, the IQ is very important, if you have low IQ you will WanTuo. Dudu’s IQ is a given number k. We use an integer vii to represent the IQ of the ith classmate.
The sum of new two teammates’ IQ must more than Dudu’s IQ.
For some reason, Dudu don’t want the two teammates comes from the same class.
Now, give you the status of classes, can you tell Dudu how many ways there are.

Input

There is a number T shows there are T test cases below. (T≤20T≤20)
For each test case , the first line contains two integers, n and k, which means the number of class and the IQ of Dudu. n ( 0≤n≤10000≤n≤1000 ), k( 0≤k<2310≤k<231 ).
Then, there are n classes below, for each class, the first line contains an integer m, which means the number of the classmates in this class, and for next m lines, each line contains an integer vii, which means there is a person whose iq is vii in this class. m( 0≤m≤1000≤m≤100 ), vii( 0≤v[i]<2310≤v[i]<231 )

Output

For each test case, output a single integer.

Sample Input

1
3 1
1 2
1 2
2 1 1

Sample Output

5

Hint

题意

看样例就知道了 改变k次0
问最大长度的连续1序列

题解:

三个特判
k==0 k==n
还有原序列k小于n 都是0的

AC代码

#include <cstdio>
#include <iostream>
#include <queue>
#include <map>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 3e5+5;
int a[N];
int sum[N];
int n,k;
bool check(int t){
    int cnt = n;
    for (int i = 1; i <= n-t+1; ++i){
        if (t>=(sum[i+t-1]-sum[i-1])){
            cnt = min(cnt,t-(sum[i+t-1]-sum[i-1]));
        }
    }
    if (cnt>k) return false;
    return true;
}
int main(){
    int aa = 1;
    int sk = 1;
    int ff = 1;
    scanf("%d%d",&n,&k);
    for (int i = 1; i <= n; ++i){
        scanf("%d",&a[i]);
        sum[i] = sum[i-1]+a[i];
        if (a[i]==a[i-1]&&a[i]==1){
            aa++;
            sk = max(aa,sk);
        }else aa = 1;
        if (a[i]==1) ff = 0;
    }
    if (ff) sk=0;
    int l = 0,r = n;
    while (l<=r){
        int mid = (l+r)>>1;
        if (!check(mid)) r = mid-1;
        else l = mid+1;
    }
    if (k==0) printf("%d\n",sk);
    else printf("%d\n",l-1);
    int fg = 0;
    int ct = 0;
    if (n==k){
        for (int i = 1; i <= n; ++i){
            if (i==1) printf("1");
            else printf(" 1");
        }
    }
    else for (int i = 1; i <= n; ++i){
            if (l-1-(sum[i+l-2]-sum[i-1])==k&&fg==0){
                fg = 1;
                ct = l;
            }
            if (ct > 1){
                if (i==1) printf("1");
                else printf(" 1");
                ct--;
            }else if(i==1) printf("%d",a[i]);
            else printf(" %d",a[i]);
        }
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值