↓ Skip to main content

Posts

2016

whust 2016 #1 H - Pair: normal and paranormal

·426 words·1 min
题目链接 其实就是括号匹配的模型。。用栈即可。。被我写挂好几发。。该死该死。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月07日 星期日 17时34分13秒 4File Name :code/whust2016/#1/HH.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10 11#include <algorithm> 12#include <vector> 13#include <queue> 14#include <stack> 15#include <set> 16#include <map> 17#include <string> 18#include <cmath> 19#include <cstdlib> 20#include <deque> 21#include <ctime> 22#include <cctype> 23#define fst first 24#define sec second 25#define lson l,m,rt<<1 26#define rson m+1,r,rt<<1|1 27#define ms(a,x) memset(a,x,sizeof(a)) 28typedef long long LL; 29#define pi pair < int ,int > 30#define MP make_pair 31 32using namespace std; 33const double eps = 1E-8; 34const int dx4[4]={1,0,0,-1}; 35const int dy4[4]={0,-1,1,0}; 36const int inf = 0x3f3f3f3f; 37const int N=1E4+7; 38char st[N]; 39int s[N]; 40int n; 41int plow[N]; 42int pup[N]; 43int ans[N]; 44stack<int>stk; 45 46 47bool ok(int x, int y) 48{ 49 // cout<<"x:"<<x<<" y:"<<y<<endl; 50 if (plow[x]>0&&pup[y]>0) 51 { 52 ans[pup[y]] = plow[x]; 53 return true; 54 } 55 if (plow[y]>0&&pup[x]>0) 56 { 57 ans[pup[x]] = plow[y]; 58 return true; 59 } 60 return false; 61} 62int main() 63{ 64 #ifndef ONLINE_JUDGE 65 freopen("code/in.txt","r",stdin); 66 #endif 67 68 cin>>n>>st; 69 n*=2; 70 int cntA = 0 ; 71 int cnta = 0 ; 72 ms(pup,0); 73 ms(plow,0); 74 for ( int i = 0 ; i < n ; i++) 75 { 76 if (st[i]>='a'&&st[i]<='z') 77 { 78 cnta++; 79 plow[i] = cnta; 80 } 81 else 82 { 83 cntA++; 84 pup[i] = cntA; 85 } 86 } 87// for ( int i = 0 ; i < n ; i++) cout<<"plow[i]:"<<plow[i]<<" pup[i]:"<<pup[i]<<endl; 88 for ( int i = 0 ; i < n ; i++) 89 { 90 st[i] = toupper(st[i]); 91 } 92// cout<<"st:"<<st<<endl; 93 ms(ans,-1); 94 while (!stk.empty()) stk.pop(); 95 stk.push(0); 96 for ( int i = 1 ; i < n ; i++) 97 { 98 while (!stk.empty()&&st[stk.top()]==st[i]&&ok(stk.top(),i)&&i<n) 99 { 100 stk.pop(); 101 i++; 102 } 103 stk.push(i); 104 } 105 for ( int i = 1 ; i <= n/2 ; i++) 106 { 107 if (ans[i]==-1) 108 { 109 puts("Impossible"); 110 return 0; 111 } 112 } 113 for ( int i = 1 ; i < n/2 ; i++) printf("%d ",ans[i]); 114 printf("%d\n",ans[n/2]); 115 116 #ifndef ONLINE_JUDGE 117 fclose(stdin); 118 #endif 119 return 0; 120}

hdu 3415 Max Sum of Max-K-sub-sequence (单调队列)

·784 words·2 mins
hdu 3415 题意:给出n个整数,是一个环(也就是a[n]右边是a[1])求一段长度不超过k的数使得和最大,问最大和是多少并给出这段数的位置。 思路:为了处理环,先把n个数复制一下就好,然后求前缀和sum[i]

ural 1126. Magnetic Storms (单调队列模板题)

·900 words·2 mins
ural 1126 题意:n个数,求从第k个元素开始,求每k个元素的最大值(一共求n-k+1次) 思路:单调队列。 单调队列学习链接 其实单调队列挺容易的理解的。。。当时觉得写不明白大概是因为看到的代码写得太丑了2333

hdu 1559 最大子矩阵 (二维前缀和)

·313 words·1 min
hdu 1559 题意:给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使子矩阵中所有元素的和最大。 思路:二维前缀和就好。。。和单调栈没有半毛钱关系吧。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月03日 星期三 21时08分18秒 4File Name :code/hdu/1559.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <stack> 14#include <set> 15#include <map> 16#include <string> 17#include <cmath> 18#include <cstdlib> 19#include <ctime> 20#define fst first 21#define sec second 22#define lson l,m,rt<<1 23#define rson m+1,r,rt<<1|1 24#define ms(a,x) memset(a,x,sizeof(a)) 25typedef long long LL; 26#define pi pair < int ,int > 27#define MP make_pair 28 29using namespace std; 30const double eps = 1E-8; 31const int dx4[4]={1,0,0,-1}; 32const int dy4[4]={0,-1,1,0}; 33const int inf = 0x3f3f3f3f; 34const int N=1E3+7; 35int a[N][N]; 36int sum[N][N]; 37int n,m,x,y; 38int main() 39{ 40 #ifndef ONLINE_JUDGE 41 freopen("code/in.txt","r",stdin); 42 #endif 43 int T; 44 cin>>T; 45 while (T--) 46 { 47 ms(sum,0); 48 scanf("%d%d%d%d",&n,&m,&x,&y); 49 for ( int i = 1 ; i <= n ; i++) 50 for ( int j = 1 ; j <= m ; j++) 51 { 52 scanf("%d",&a[i][j]); 53 sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1] + a[i][j]; 54 } 55 int ans = 0 ; 56 for ( int i = 1 ; i <= n-x+1 ; i++) 57 for ( int j = 1 ; j <= m-y+1 ; j++) 58 ans = max(ans,sum[i+x-1][j+y-1]-sum[i-1][j+y-1]-sum[i+x-1][j-1]+sum[i-1][j-1]); 59 60 printf("%d\n",ans); 61 } 62 63 #ifndef ONLINE_JUDGE 64 fclose(stdin); 65 #endif 66 return 0; 67}

poj 2796 Feel Good (前缀和,单调栈)

·509 words·2 mins
poj 2796 题意:给出一个人n(1E5)天的情绪值(0..1E6),一段时间的value的定义是这段时间的情绪之和*这段时间情绪的最小值。 现在求value的最大值,并且输出得到这个最大值的区间。

poj 2082 Terrible Sets (前缀和,单调栈)

·444 words·1 min
poj 2082 题目链接 题意:这道题简直就是。。。教给大家怎么把一句话把简单的题让人出得看不懂。。。真的一点意思都没有。给出n个矩形的宽度和高度,这些矩形并排顺次排列在x轴上,问最大面积。

poj 1964 City Game(单调栈,输入挂)

·1006 words·3 mins
poj 1964 题意:n*m 的 maze,由 ‘R’ 和 ‘F’ 组成,现在要求找到面积最大的矩形,使得矩形中所有格子都是 ‘F’。 思路:单调栈。一开始神 tm TLE 了,复杂度明明没问题啊。 结果看到有人说这题数据量比较大,scanf 会超时,所以要用输入挂,getchar 什么的。

poj 3250 Bad Hair Day(单调栈)

·442 words·1 min
poj 3250 题意: n头牛排成一列,第n只牛在最前面,第1只牛在最后面。第i只牛能看到的牛的个数是,它前面的且没有被其他牛遮挡的牛的个数,遮挡的条件是高度大于或者相同。现在问所有牛能看到的牛的个数的和。

poj 2559 Largest Rectangle in a Histogram (单调栈)

·657 words·2 mins
poj 2559 题意:给定从左到右多个矩形,已知这此矩形的宽度都为1,长度不完全相等。这些矩形相连排成一排,求在这些矩形包括的范围内能得到的面积最大的矩形,求该面积。所求矩形可以横跨多个矩形,但不能超出原有矩形所确定的范围。

codeforces 123 D. String (后缀数组+两次二分得到区间+rmq)

·1380 words·3 mins
题目链接 题意:定义一个函数 F。 For example: F(babbabbababbab, babb) = 6. The list of pairs is as follows: (1, 4), (4, 7), (9, 12) Its continuous sequences are: (1, 4) (4, 7) (9, 12) (1, 4), (4, 7) (4, 7), (9, 12) (1, 4), (4, 7), (9, 12) 二分。 题目描述得很烂,看例子吧,大概就是:如果字符串 y 在字符串 x 中出现 n 次,那么 F(x,y)=n*(n+1)/2

poj 2406 Power Strings (后缀数组||kmp)

·1756 words·4 mins
poj 2406 题意:给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的, 求 R 的最大值 思路:论文题。 转载论文中的题解: 做法比较简单,穷举字符串 S 的长度 k,然后判断是否满足。判断的时候, 先看字符串 L 的长度能否被 k 整除,再看 suffix(1) 和 suffix(k+1) 的最长公共前缀是否等于 n-k。在询问最长公共前缀的时候,suffix(1) 是固定的,所以 RMQ 问题没有必要做所有的预处理,只需求出 height 数组中的每一个数到 height[rank[1]] 之间的最小值即可。整个做法的时间复杂度为 O(n)

spoj SUBST1 - New Distinct Substrings(后缀数组)

·599 words·2 mins
题目连接 题意:求所有不同的子串个数。 思路:后缀数组。和上一道题一样,就是数据范围变成了 5E4…1A 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月02日 星期二 18时32分28秒 4File Name :code/spoj/subst1.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <set> 13#include <map> 14#include <string> 15#include <cmath> 16#include <cstdlib> 17#include <ctime> 18#define fst first 19#define sec second 20#define lson l,m,rt<<1 21#define rson m+1,r,rt<<1|1 22#define ms(a,x) memset(a,x,sizeof(a)) 23typedef long long LL; 24#define pi pair < int ,int > 25#define MP make_pair 26using namespace std; 27const double eps = 1E-8; 28const int dx4[4]={1,0,0,-1}; 29const int dy4[4]={0,-1,1,0}; 30const int inf = 0x3f3f3f3f; 31const int N=5E4+7; 32char s[N]; 33int sa[N],rk[N],t[N],t2[N],cnt[N],height[N]; 34int cmp (int *r,int a,int b,int l){ return r[a]==r[b]&&r[a+l]==r[b+l];} 35void getSa(int n,int m) 36{ 37 int *x=t,*y=t2; 38 ms(cnt,0); 39 for ( int i = 0 ; i < n ; i++) cnt[x[i]=s[i]]++; 40 for ( int i = 0 ; i < m ; i++) cnt[i]+=cnt[i-1]; 41 for ( int i = n-1 ; i >= 0 ; i--) sa[--cnt[x[i]]] = i; 42 for ( int k = 1 ; k <= n ; k<<=1) 43 { 44 int p = 0; 45 for ( int i = n-k ; i < n ; i++) y[p++] = i; 46 for ( int i = 0 ; i < n; i++) if (sa[i]>=k) y[p++] = sa[i]-k; 47 ms(cnt,0); 48 for ( int i = 0 ; i <n ; i++) cnt[x[y[i]]]++; 49 for ( int i = 0 ;i < m ; i++) cnt[i]+=cnt[i-1]; 50 for ( int i = n-1 ; i >= 0 ; i--) sa[--cnt[x[y[i]]]] = y[i]; 51 swap(x,y); 52 p = 1; 53 x[sa[0]] = 0; 54 for ( int i = 1 ; i < n ; i++ ) 55 x[sa[i]] = cmp(y,sa[i-1],sa[i],k)?p-1:p++; 56 if (p>=n) break; 57 m = p; 58 } 59} 60void getHeight( int n) 61{ 62 int k = 0 ; 63 for ( int i = 0 ; i <n ; i ++) rk[sa[i]] = i ; 64 height[0] = 0 ; 65 for ( int i = 0 ; i < n ; i++) 66 { 67 if (rk[i]==0) continue; 68 if (k) k--; 69 int j = sa[rk[i]-1]; 70 while (s[i+k]==s[j+k]) k++; 71 height[rk[i]] = k; 72 } 73} 74int getSuffix(char s[]) 75{ 76 int len = strlen(s); 77 int up = 0 ; 78 for ( int i = 0 ; i < len ; i++) 79 { 80 int val = s[i]; 81 up = max(up,val); 82 } 83 s[len++]='$'; 84 getSa(len,up+1); 85 getHeight(len); 86 return len; 87} 88int solve ( int n) 89{ 90 ms(cnt,0); 91 int up = 0; 92 for ( int i = 0 ;i <n ; i ++) cnt[height[i]]++, up = max(up,height[i]); 93 for ( int i = up-1 ; i >= 1; i--) cnt[i]+=cnt[i+1]; 94 int res = 0 ; 95 for ( int i = 1 ; i <=up ; i++) 96 res+=cnt[i]; 97 return res; 98} 99int main() 100{ 101 #ifndef ONLINE_JUDGE 102 freopen("code/in.txt","r",stdin); 103 #endif 104 int T; 105 scanf("%d",&T); 106 while (T--) 107 { 108 scanf("%s",s); 109 int len = getSuffix(s); 110 LL ans = 1LL*len*(len-1)/2; 111 ans-=1LL*solve(len); 112 printf("%lld\n",ans); 113 } 114 #ifndef ONLINE_JUDGE 115 fclose(stdin); 116 #endif 117 return 0; 118}

spoj DISUBSTR - Distinct Substrings (统计字串个数,后缀数组)

·1022 words·3 mins
题目链接 题意:给出一个字符串,问所有不同的字串的个数。 思路:直接求比较困难。我们考虑,假如组成字符串的所有字符都不相同,那么就没有相同的字串。假设字符串的长度为 n,那么长度为 1 的子串有 n 个,为 2 的有 n-1 个……为 n 的有 1 个,一共就是 n*(n+1)/2 个,但是实际上会有重复的。

poj 3261 Milk Patterns (最长公共子串,后缀数组)

·813 words·2 mins
poj3261 题意:给一个字符串,要求找出至少出现k次的最长重复子串… 思路:后缀数组,然后再次用到了根据height数组对后缀进行分组的套路…二分判定合法性,对于当前的最长长度x,分组使得每组中的height[i]都大于等于x,所不同的是,判定变成了存在一个组,后缀的个数至少为k个(因为这样,就可以对于大于等于k个的后缀,同时取前x长度,得到的就是出现了至少k次且长度为x的前缀)1A,蛤蛤蛤

hdu 1280 前m大的数 (计数排序)

·474 words·1 min
hdu1280 题意:给出n(3000)个数,两两求和,输出最大的m(5000)个和。 思路:由于数据有限,想到计数排序。。。以及,m个可能刚好某个数据没有全部输出,要在while里判断一下。。