[算法竞赛入门经典] UVA 12174 - Shuffle
来源:互联网 发布:美工设计电脑配置 编辑:程序博客网 时间:2024/05/16 06:54
紫书P247 例题8-15
先预处理一遍每个窗口内的每个数字是否只出现一次,结果存到flag数组里。
还有个特殊构造的地方,由于首尾窗口可能存在不完整的情况,那么我们在n个数后面在添加s个元素,尾部窗口就不会出现不完整的情况了。
然后直接枚举求出结果。
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <algorithm> #include <iostream> #include <set> #include <map> #include <queue> #include <stack>#include <assert.h>typedef long long LL;using namespace std;int a[200001], b[300001], flag[200001];int main() { //freopen("test0.in", "r", stdin); //freopen("test0.out", "w", stdout); int T, s, n, ans; scanf("%d", &T); while(T--) { scanf("%d %d", &s, &n); for(int i = 0; i < n; ++i) { scanf("%d", &a[i]); } for(int i = n; i < n + s; ++i) { a[i] = 100001+i-n; } memset(flag, 0, sizeof(flag)); memset(b, 0, sizeof(b)); int cnt = 0; for(int i = 0; i < n+s; ++i) { if(i >= s) { --b[a[i-s]]; if(b[a[i-s]] >= 1) { --cnt; } } ++b[a[i]]; if(b[a[i]] == 1 && cnt == 0) { flag[i] = 1; } else if(b[a[i]] > 1) { ++cnt; } } ans = 0; for(int i = 0; i < s; ++i) { int flg = 1; for(int j = i; j < n+s; j += s) { if(!flag[j]) { flg = 0; break; } } if(flg) { ++ans; } } printf("%d\n", ans); } return 0; }
0 0
- [算法竞赛入门经典] UVA 12174 - Shuffle
- UVA-230《算法竞赛入门经典》
- 算法竞赛入门经典 UVa 514 Rails
- 算法竞赛入门经典
- 算法竞赛入门经典
- [算法竞赛入门经典] UVA 1471 - Defense Lines
- [算法竞赛入门经典] UVA 714 - Copying Books
- (DS 《算法竞赛入门经典》)UVA 11997 K Smallest Sums
- AOAPC-I: 算法竞赛入门经典 UVa 习题集分类
- UVa 11426--算法竞赛入门经典训练指南
- 16 - 11 - 23 得分 - UVA 1585 --《算法竞赛入门经典》
- 算法竞赛入门经典心得
- 《算法竞赛入门经典》勘误表
- 算法竞赛入门经典读书笔记
- #《算法竞赛入门经典》勘误
- 算法竞赛入门经典2
- 算法竞赛入门经典 exe_2
- 算法竞赛入门经典第一章
- 仿酷狗音乐播放器开发日志二十三 修复Option控件显示状态不全的bug(附源码)
- Unable to execute dex: Multiple dex files define 解决方法
- cocos2dx 3.2从零学习(六)——CocosStudio(VS2013工程导入及环境设置)
- Category隐藏UIImagePickerController的StatusBar
- lightbird json not JSON serializable
- [算法竞赛入门经典] UVA 12174 - Shuffle
- 求两个数的最大公约数——辗转相除法
- 写个文章更新下
- AndroidManifest.xml配置文件详解
- Java线程和进程
- 设置_ITERATOR_DEBUG_LEVEL
- HDU 1087 Super Jumping! Jumping! Jumping! 动态规划
- SPOJ 16637. Non-Square Free Numbers
- 经纬度查询