↓ Skip to main content
  1. Posts/

codeforces 660 C. Hard Process (ruler)

·321 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

cf660C

solution:ruler.1A

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年06月08日 星期三 23时43分18秒
  4File Name :code/cf/problem/660C.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=3E5+7;
 34int n,k;
 35int sum[N],a[N];
 36
 37void ruler()
 38{
 39    int head = 1;
 40    int tail = 1;
 41    int l,r;
 42    int  res = -1;
 43    int cnt = 0 ;
 44
 45    while (tail<=n)
 46    {
 47	while (a[tail]==1) tail++;
 48//	cout<<"head:"<<head<<" tail:"<<tail<<endl;
 49	if (a[tail]==0&&tail<=n) cnt++;
 50
 51	while (sum[tail]-sum[head-1]<=k&&tail<=n) tail++;
 52//	cout<<"head:"<<head<<"tail:"<<tail<<endl;
 53	    if (tail-head>res)
 54	    {
 55		res = tail-head;
 56	//	cout<<"res:"<<res<<endl;
 57		l = head;
 58		r = tail-1;
 59	    }
 60
 61	while (head<=tail&&sum[tail]-sum[head-1]>k) head++;
 62//	cout<<"head::"<<head<<" tail:"<<tail<<endl;
 63	if (tail<=n&&tail-head+1>res)
 64	{
 65	    res = tail-head+1;
 66	    l = head;
 67	    r = tail;
 68	}
 69
 70
 71    }
 72
 73
 74    for ( int i = l ; i <= r ; i++) a[i] = 1;
 75
 76    cout<<res<<endl;
 77    for ( int i = 1 ; i <= n ; i++) cout<<a[i]<<" ";
 78}
 79int main()
 80{
 81	#ifndef  ONLINE_JUDGE
 82	freopen("code/in.txt","r",stdin);
 83  #endif
 84
 85	cin>>n>>k;
 86
 87	sum[0] = 0;
 88	for ( int i = 1; i <= n ; i++)
 89	{
 90	    scanf("%d",&a[i]);
 91	    sum[i] = sum[i-1] +(1-a[i]);
 92	}
 93
 94	ruler();
 95
 96
 97  #ifndef ONLINE_JUDGE
 98  fclose(stdin);
 99  #endif
100    return 0;
101}

Related

hdu 3530 Subsequence (尺取+rmq)

·425 words·1 min
hdu 3530题目链接 题意:给出n个数,m,k,问最大的j-i+1,使得【i,j】间的最大值与最小值的差属于[m,k] 思路:rmq+尺取。 2A. 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年05月19日 星期四 16时52分03秒 4File Name :code/hdu/3530.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=1E5+7; 34int n; 35int a[N]; 36int dp[N][20],dp2[N][20]; 37int m,k; 38 39void rmq_init() 40{ 41 for ( int i = 1 ; i <= n ; i ++) 42 dp[i][0] = dp2[i][0] = a[i]; 43 44 for ( int j = 1 ; (1<<j) <= n ; j++) 45 for ( int i = 1 ; i + (1<<j) -1 <= n ; i++) 46 { 47 dp[i][j] = max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); 48 dp2[i][j]=min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]); 49 } 50} 51 52int rmq( int l,int r) 53{ 54 int k = 0 ; 55 while (1<<(k+1)<=r-l+1) k++; 56 int mx = max(dp[l][k],dp[r-(1<<k)+1][k]); 57 int mn = min(dp2[l][k],dp2[r-(1<<k)+1][k]); 58 59 return mx-mn; 60} 61 62int ruler() 63{ 64 int head = 1; 65 int tail = 1; 66 int res = -1 ; 67 while (tail<=n) 68 { 69 int cur = rmq(head,tail); 70 while (head<tail&&rmq(head,tail)>k) head++; 71 while (tail<n&&rmq(head,tail)<m) tail++; 72 //if (tail>n) break; 73 cur = rmq(head,tail); 74 if (cur>=m&&cur<=k) 75 { 76 res = max(res,tail-head); 77 } 78// cout<<"head:"<<head<<" tail:"<<tail<<" cur:"<<cur<<" res:"<<res<<endl; 79 tail++; 80 81 } 82 return res+1; 83} 84int main() 85{ 86 #ifndef ONLINE_JUDGE 87 freopen("code/in.txt","r",stdin); 88 #endif 89 90 while (scanf("%d %d %d",&n,&m,&k)!=EOF) 91 { 92 ms(dp,0); 93 for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]); 94 if (m>k) 95 { 96 puts("0"); 97 continue; 98 } 99 rmq_init(); 100 printf("%d\n",ruler()); 101 } 102 103 104 105 #ifndef ONLINE_JUDGE 106 fclose(stdin); 107 #endif 108 return 0; 109}

codeforces 279 B books

·296 words·1 min
http://codeforces.com/problemset/problem/279/B 题意:给定一个序列,问一段连续的序列的和小于等于t的最长的序列的长度。 思路:尺取法。三个月前学习的了。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2015年12月15日 星期二 20时08分16秒 4File Name :code/cf/problem/279B.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=1E5+7; 34int n; 35int t; 36int a[N]; 37 38void solve() 39{ 40 int head = 1; 41 int tail = 1; 42 int sum = 0 ; 43 int ans = 0 ; 44 while (tail<=n) 45 { 46// cout<<"aaaaa?"<<endl; 47 sum = sum + a[tail]; 48// cout<<"head:"<<head<<" tail:"<<tail<<" sum:"<<sum<<endl; 49 if (sum>t) 50 { 51 while (sum>t) 52 { 53 sum = sum - a[head]; 54// cout<<"sum:"<<sum<<endl; 55 head++; 56 } 57 } 58 if (sum<=t) 59 ans = max(ans,tail-head+1); 60 tail++; 61 62 } 63 printf("%d\n",ans); 64} 65int main() 66{ 67 #ifndef ONLINE_JUDGE 68 freopen("code/in.txt","r",stdin); 69 #endif 70 71 scanf("%d%d",&n,&t); 72 for ( int i =1 ; i <= n ; i++) scanf("%d",&a[i]); 73 solve(); 74 75 #ifndef ONLINE_JUDGE 76 fclose(stdin); 77 #endif 78 return 0; 79}

codeforces #321 div 2 B. Kefa and Company(尺取法)

·654 words·2 mins
B. Kefa and Company time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn’t want any friend to feel poor compared to somebody else in the company (Kefa doesn’t count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!

poj 2739 Sum of Consecutive Prime Numbers (尺取法)

·426 words·1 min
一开始迷之wa… 先找出素数下标的上界就可以A… 然后纠结了20分钟... 然后发现是预处理的素数少了一个素数.. 我预处理是处理到<10005的素数... 最大数10000,而超过10000的第一个素数是10007 这样判断终止条件就会死循环… sad

poj 2100 Graveyard Design (two pointers ,尺取法)

·314 words·1 min
不多说,直接代码。 代码实现 1/************************************************************************* 2 > File Name: code/poj/2100.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年09月25日 星期五 00时42分49秒 6 ************************************************************************/ 7 8#include<iostream> 9#include<iomanip> 10#include<cstdio> 11#include<algorithm> 12#include<cmath> 13#include<cstring> 14#include<string> 15#include<map> 16#include<set> 17#include<queue> 18#include<vector> 19#include<stack> 20#include<cctype> 21#define y1 hust111qqz 22#define yn hez111qqz 23#define j1 cute111qqz 24#define ms(a,x) memset(a,x,sizeof(a)) 25#define lr dying111qqz 26using namespace std; 27#define For(i, n) for (int i=0;i<int(n);++i) 28typedef long long LL; 29typedef double DB; 30const int inf = 0x3f3f3f3f; 31LL n; 32LL maxn; 33vector<LL>ans; 34 35 36void solve() 37{ 38 LL head = 1,tail = 1; 39 LL sum = 0 ; 40 41 while (tail<=maxn) 42 { 43 sum = sum + tail*tail; 44 45 if (sum>=n) 46 { 47 while (sum>n)//主要是while,因为可能要减掉多个才能小于n 48 { 49 sum -= head*head; 50 head++; 51 } 52 if (sum==n) 53 {//因为要先输出答案个数...所以必须存起来延迟输出... 54 ans.push_back(head); 55 ans.push_back(tail); 56 } 57 } 58 tail++; 59 } 60 LL sz = ans.size(); 61 printf("%lld\n",sz/2); 62 for (LL i = 0 ; i<ans.size() ; i = i +2) 63 { 64 printf("%lld",ans[i+1]-ans[i]+1); 65 for ( LL j = ans[i] ; j <=ans[i+1] ; j++) printf(" %lld",j); 66 printf("\n"); 67 } 68 69} 70int main() 71{ 72 #ifndef ONLINE_JUDGE 73 freopen("in.txt","r",stdin); 74 #endif 75 scanf("%lld",&n); 76 maxn = ceil(sqrt(n)); 77 solve(); 78 79 #ifndef ONLINE_JUDGE 80 fclose(stdin); 81 #endif 82 return 0; 83}