链接:http://codeforces.com/problemset/problem/497/B
这种题考察的是基本功。。像我这种基本功为0的喳喳只能卡在这里了。。。
思路:n范围为10^5, 必须找出nlogn的算法才行。枚举t,然后用二分找出解。。巧妙~~ 好题赞一个,虽然不会做~~~
代码:
/* *********************************************** Author :ltwy Created Time :2014年12月18日 星期四 16时28分12秒 File Name :1.cpp ************************************************ */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; int n; int a[101000]; int sum1[101000]; int sum2[101000]; pair<int, int> p[100100]; int get_next(int s, int st, int ed) { if(sum1[ed] - sum1[st-1] <s && sum2[ed] - sum2[st-1] < s) return -1; int l = st, r = ed; int id = ed; while(l <= r) { int mid = (l+r)>>1; if(sum1[mid] - sum1[st-1] < s&& sum2[mid] - sum2[st-1] < s) { l = mid+1; } else { id = mid; r = mid -1; } } return id; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d", &n); for(int i=1; i<=n; i++) { scanf("%d", &a[i]); } memset(sum1, 0, sizeof(sum1)); memset(sum2, 0, sizeof(sum2)); int T1 = 0, T2 = 0; for(int i = 1; i<=n; i++) { if(a[i] == 1) T1++; if(a[i] == 2) T2++; sum1[i] = T1; sum2[i] = T2; } int cnt = 0; int W1 = 0; int W2 = 0; bool flag; for(int s=1; s<=n; s++) { W1 = 0; W2 = 0; flag = false; if(sum1[n] < s && sum2[n] < s) break; int pos = 1; int last = 0; while(pos <= n) { int temp = get_next(s, pos, n); if(temp == -1) { flag = true; break; } if(a[temp] == 1) { W1++; last = 1; } else { W2++; last = 2; } pos = temp+1; } if(flag) continue; if(W1 == W2) continue; if(W1 > W2&& last == 2) continue; if(W2 > W1&& last == 1) continue; p[cnt++] = make_pair(max(W1, W2), s); } printf("%d\n", cnt); sort(p, p+cnt); for(int i=0; i<cnt; i++) printf("%d %d\n", p[i].first, p[i].second); return 0; }
时间: 2024-11-05 04:49:28