Hard Process(二分)
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
You are given an array a with n elements. Each element of a is either 0 or 1.
Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k.
The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
Output
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones.
On the second line print n integers aj — the elements of the array a after the changes.
If there are multiple answers, you can print any one of them.
Sample Input
7 1 1 0 0 1 1 0 1
4 1 0 0 1 1 1 1
10 2 1 0 0 1 0 1 0 1 0 1
5 1 0 0 1 1 1 1 1 0 1
题解:让改变k个数,使序列连续1的个数最大,我们可以求0的前缀和,然后通过二分来找位置;我竟然用暴力超时了好长时间。。。
二分:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN = ;
int num[MAXN];
int a[MAXN];
int L, n, k; int js(int x){
for(int i = ; i + x <= n; i++){
if(num[i + x] - num[i] <= k){
L = i + ;
return true;
}
}
return false;
}
int erfen(int l, int r){
int mid, ans;
while(l <= r){
mid = (l + r) >> ;
if(js(mid)){
ans = mid;
l = mid + ;
}
else
r = mid - ;
}
return ans;
}
int main(){
while(~scanf("%d%d",&n, &k)){
int temp;
memset(num, , sizeof(num));
for(int i = ; i <= n; i++){
scanf("%d", a + i);
num[i] = num[i - ] + (a[i] == );
}
int ans = erfen(,n);
for(int i = L; i < L + ans; i++){
a[i] = ;
}
printf("%d\n", ans);
for(int i = ; i <= n; i++){
if(i != )printf(" ");
printf("%d",a[i]);
}puts("");
}
return ;
}
暴力超时:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN = ;
int num[MAXN];
int pos[MAXN];
int p[MAXN];
int main(){
int n, k;
while(~scanf("%d%d",&n, &k)){
int gg = ;
for(int i = ; i < n; i++){
scanf("%d", num + i);
if(num[i] == )gg = ;
}
if(!gg){
printf("%d\n",k);
for(int i = ; i < k; i++){
if(i)printf(" ");
printf("");
}
for(int i = k; i < n; i++){
if(i)printf(" ");
printf("");
}puts("");
continue;
}
int kg = , ans = , temp = , cnt = , tp = ;
for(int i = ; i < n; i++){
if(kg == && num[i] == ){
temp = ;
kg = ;
cnt = ;
for(int j = i; j < n; j++){ if(num[j] == ){
temp++;
}
else{
if(cnt + > k)break;
pos[cnt++] = j;
temp++;
}
}
int j = i;
while(cnt < k && j > ){
pos[cnt++] = --j;
temp++;
} if(ans < temp){
ans = temp;
tp = cnt;
for(int j = ; j < tp; j++){
p[j] = pos[j];
}
}
}
if(num[i] == )kg = ;
}
for(int i = ; i < tp; i++){
// printf("%d ",p[i]);
num[p[i]] = ;
}//puts("");
printf("%d\n", ans);
for(int i = ; i < n; i++){
if(i)printf(" ");
printf("%d",num[i]);
}
puts("");
}
return ;
}
Hard Process(二分)的更多相关文章
- hdu 3433 A Task Process 二分+dp
A Task Process Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...
- codeforces 660C C. Hard Process(二分)
题目链接: C. Hard Process time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces 660C - Hard Process - [二分+DP]
题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...
- hdu3433A Task Process( 二分dp)
链接 二分时间,在时间内dp[i][j]表示截止到第i个人已经做了j个A最多还能做多少个B #include <iostream> #include<cstdio> #incl ...
- 二分+DP HDU 3433 A Task Process
HDU 3433 A Task Process Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- HDU 3433 (DP + 二分) A Task Process
题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i ...
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...
- hdu 3433 A Task Process(dp+二分)
题目链接 题意:n个人, 要完成a个x任务, b个y任务. 求,最短的时间 思路:由于时间较大,用 二分来找时间. dp[i][j]表示 i个人完成j个x任务, 最多能完成的y任务个数 这个题 不是很 ...
随机推荐
- iOS开发-Runtime详解(简书)
简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [receiver message]; // ...
- redis简单配置
由于前段时间使用Kestrel,同时要操作Memcached及时更新缓存,又要操作database,持久化数据. 貌似Redis既可以当Cache又可以当Queue!于是,今天开始研究Redis! 一 ...
- COCOS2D-X 不反复随机数
srand(time(NULL)); int a[5]; for(int i=0;i<5;i++) { a[i]=CCRANDOM_0_1()*5; } srand放在循环外面
- LeetCode227:Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Swift——(一)为Swift内置类型加入属性
在看苹果官方的Swift Language的时候,遇到实验:Write an extension for the Double type that add an absoluteValue prope ...
- CBitmap,HBitmap,Bitmap区别及联系
加载一位图,可以使用LoadImage: HANDLE LoadImage(HINSTANCE hinst,LPCTSTR lpszName,UINT uType,int cxDesired,int ...
- HTML 表单与输出
我们先来设置一个简单的表单 <!doctype html><html><head> <meta charset="utf-8"> & ...
- 利用IIS7 解决URL访问限制问题
网站可以通过URl直接访问一些不希望被访问的东西, 比如一些图片,js,css等等. 为了解决这个问题看了好多文章,不过毕竟我是新手菜鸟级别的,没有具体的解决方法,真心不知道怎么弄. 今天在看IIS的 ...
- (转)ASP.Net 学习路线
入门篇 1.学习面向对象(OOP)的编程思想 许多高级语言都是面向对象的编程,.NET也不例外.如果您第一次接触面向对象的编程,就必须理解类.对象.字段.属性.方法和事件.封装.继承和多态性.重载.重 ...
- URI, URL, and URN
URI: uniform resource identifier,统一资源标识符,用来唯一的标识一个资源. URL: uniform resource locator,统一资源定位器,它是一种具体的U ...