Codeforces 660 C. Hard Process (尺取)

题目链接:http://codeforces.com/problemset/problem/660/C

尺取法

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     static int num[int(3e5 + 5)];
 6     int n, k;
 7     scanf("%d %d", &n, &k);
 8     for(int i = 1; i <= n; ++i) {
 9         scanf("%d", num + i);
10     }
11     int l = 1, res = 0, cnt = 0, flagl = 0, flagr = -1;
12     for(int i = 1; i <= n; ++i) {
13         if(num[i]) {
14             if(i - l + 1 > res) {
15                 res = i - l + 1, flagl = l, flagr = i;
16             }
17         } else {
18             cnt++;
19             while(cnt > k && l <= i) {
20                 if(!num[l++])
21                     --cnt;
22             }
23             if(i - l + 1 > res) {
24                 res = i - l + 1, flagl = l, flagr = i;
25             }
26         }
27     }
28     for(int i = flagl; i <= flagr; ++i)
29         num[i] |= 1;
30     printf("%d\n", res);
31     for(int i = 1; i <= n; ++i) {
32         printf("%d%c", num[i], i == n ? ‘\n‘: ‘ ‘);
33     }
34     return 0;
35 }
时间: 2024-08-15 01:39:53

Codeforces 660 C. Hard Process (尺取)的相关文章

[2016-04-09][codeforces][660][C][Hard Process]

时间:2016-04-09 23:59:40 星期六 题目编号:[2016-04-09][codeforces][660][C][Hard Process] 题目大意:给定一个0 1序列,在最多把k个0改成1的情况下,最长的1子串有多长, 分析: 二分答案 check():枚举长度为mid的子串,判断里面的0的数目是否小于等于1, #include<cstdio> using namespace std; const int maxn = 3*1E5 + 10; int a[maxn],b[m

Codeforces - 1324D - Pair of Topics(尺取)

题目链接 题目大意:让你找出所有\(ai+aj > bi+bj\)(i > j) ??其实这题不用在意\(i > j\)只要把\(i\neq j\)并且符合条件的一对数记做一个答案就行了...(显然通过\(i和j\)交换必有\(i > j\)).然后我们把\(ai+aj > bi+bj\)变形可得\((ai-bi)+(aj-aj) > 0\)所以我们把所有的\(ai\)和\(bi\)做差 得到数组\(d\),那么只有\(d\)数组中存在一对数满足两数之和大于0就是答案的

Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)

题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序列的长度是多少. 将相同颜色的下标存到对应颜色的容器中,比如ans[a[i]].push_back(i)就是将下标为i颜色为a[i]的数存到ans[a[i]]容器中. 对于每种颜色序列,尺取一下 在差距小于k的情况下取能取到的最大长度. 1 //#pragma comment(linker, "/S

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has got a robot which is situated on an infinite Cartesian plane, i

Codeforces 939E Maximize! (三分 || 尺取)

<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-mean(s)$表示这个集合的最大值与平均值的最大差值. 解题分析:首先,因为输入的$x$是非递减的,所以要使$max(s)-mean(s)$最大,肯定$max(s)$就是最后一个输入元素的大小.$x$已经确定了,现在就是尽可能的使$mean(s)$尽可能的小.如何使得平均值最小呢?肯定是从最前面的最小的元素开始

Codeforces Round #451 (Div. 2)【A,B,C,D,E】【C题:模拟 D题:尺取+贪心 E题:思维+优先队列维护最值】

特判最后一位即可 1 #include<bits/stdc++.h> 2 3 using namespace std; 4 #define int long long 5 6 signed main(){ 7 int n;cin>>n;int t=n%10; 8 if(t==0) cout<<n; 9 else if(t>5) { 10 cout<<(n+10-t); 11 } 12 else { 13 cout<<(n-t); 14 }

[2016-04-09][codeforces][660][A][ Co-prime Array]

时间:2016-04-09 22:50:56 星期六 题目编号:[2016-04-09][codeforces][660][A][ Co-prime Array] 题目大意:给定一个数列,问至少需要插入多少个1 1091 109中的任一数字,才能使得相邻两个数字是互质的,输出最少次数和最后的数列 分析:直接扫一遍,相邻元素不互质的,中间插个1, #include<cstdio> #include<vector> using namespace std; const int maxn

[2016-04-09][codeforces][660][B][Seating On Bus]

时间:2016-04-09 23:29:47 星期六 题目编号:[2016-04-09][codeforces][660][B][Seating On Bus] 题目大意:按指定顺序入座,按指定顺序出座,问最后出座的顺序 分析:直接4个queue模拟一遍 #include<cstdio> #include<queue> using namespace std; queue<int> q[4]; int main(){ int n,m; scanf("%d%d&

POJ3061 Subsequence 尺取or二分

Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements o