http://acm.hdu.edu.cn/showproblem.php?pid=3474单调队列,又是第一次使用,本人蒟蒻无比啊。。。
hdu 3474 1#include <cstdio> 2#include <iostream> 3#include <cmath> 4#include <complex> 5#include <algorithm> 6#include <cstring> 7#include <queue> 8using namespace std; 9 10const int maxn = 1000000 + 10; 11int Q[maxn<<1], sum[maxn<<1]; 12char neck[maxn<<1]; 13int vis[2][maxn]; 14 15void solve( int n, int m, int v ) 16{ 17 sum[0] = 0; 18 for( int i = 1; i < m; i++ ) 19 sum[i] = sum[i-1] + ( neck[i] == 'C' ? 1 : -1 ); 20 int head = 0, tail = 0; 21 for( int i = 0; i < m; i++ ) 22 { 23 while( head < tail && sum[Q[tail-1]] >= sum[i] ) tail--; 24 Q[tail++] = i; 25 if( i >= n ) 26 { 27 while( i - Q[head] >= n ) head++; 28 vis[v][i-n] = ( sum[i-n] <= sum[Q[head]] ); 29 } 30 } 31} 32 33int main(int argc, char *argv[]) 34{ 35 int t; 36 scanf("%d",&t); 37 for( int p = 1; p <= t; p++ ) 38 { 39 scanf("%s",neck+1); 40 int n = strlen( neck + 1 ); 41 int m = n * 2 + 1; 42 for( int i = 1; i <= n; i++ ) 43 neck[i+n] = neck[i]; 44 neck[m] = 0; 45 memset( vis, 0, sizeof( vis ) ); 46 solve( n, m, 0 ); 47 reverse( neck + 1, neck + m ); 48 solve( n, m, 1 ); 49 int ans = 0; 50 for( int i = 1; i <= n; i++ ) 51 if( vis[0][i] || vis[1][n-i] ) ans ++; 52 printf("Case %d: %d\n",p,ans); 53 } 54 return 0; 55} 56
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
常用链接
留言簿
随笔分类
随笔档案
传送门
搜索
最新评论
阅读排行榜
评论排行榜
|
|