uva 11536 - Smallest Sub-Array(two pointers)
来源:互联网 发布:ddg1000数据 编辑:程序博客网 时间:2024/05/01 00:24
Smallest Sub-Array
Input: Standard Input
Output: Standard Output
Consider an integer sequence consisting of N elements where –
X1 = 1
X2 = 2
X3 = 3
Xi = (Xi-1 + Xi-2 + Xi-3) % M + 1 for i = 4 to N
Find 2 values a and b so that the sequence (Xa Xa+1 Xa+2 ... Xb-1 Xb) contains all the integers from [1,K]. If there are multiple solutions then make sure (b-a) is as low as possible.
In other words, find the smallest subsequence from the given sequence that contains all the integers from 1 to K.
Consider an example where N = 20, M = 12 and K = 4.
The sequence is {1 2 3 7 1 12 9 11 9 6 3 7 5 4 5 3 1 10 3 3}.
The smallest subsequence that contains all the integers {1 2 3 4} has length 13 and is highlighted in the following sequence:
{1 2 3 7 1 12 9 11 9 6 3 7 5 4 5 3 1 10 3 3}.
Input
First line of input is an integer T(T<100) that represents the number of test cases. Each case consists of a line containing 3 integers N(2 <N < 1000001), M(0 < M < 1001) and K(1< K < 101). The meaning of these variables is mentioned above.
Output
For each case, output the case number followed by the minimum length of the subsequence. If there is no valid subsequence, output “sequence nai” instead. Look at the sample for exact format.
Sample Input Output for Sample Input
2
20 12 4
20 12 8
Case 1: 13
Case 2: sequence nai
Problemsetter: Sohel Hafiz
Special Thanks: Md. Arifuzzaman Arif
#include <cstdio>#include <algorithm>#include <vector>#include <map>#include <queue>#include <iostream>#include <stack>#include <set>#include <cstring>#include <stdlib.h>#include <cmath>using namespace std;typedef long long LL;typedef pair<int, int> P;const int maxn = 1000000 + 5;const int INF = 1000000000;int n, m, k;int x[maxn];int vis[maxn];void pre(){ x[1] = 1;x[2] = 2;x[3] = 3; for(int i = 4;i <= n;i++) x[i] = (x[i-1]+x[i-2]+x[i-3])%m+1;}int main(){ int t, kase = 0; scanf("%d", &t); while(t--){ kase++; scanf("%d%d%d", &n, &m, &k); pre(); memset(vis, 0, sizeof(vis)); int pos = 1; int ans = INF; int cnt = 0; for(int i = 1;i <= n;i++){ vis[x[i]]++; if(x[i] <= k && vis[x[i]] == 1) cnt++; while(vis[x[pos]] > 1 || x[pos] > k){// x[pos]>k !!! vis[x[pos]]--; pos++; } if(cnt == k) ans = min(ans, i-pos+1); } if(ans != INF) printf("Case %d: %d\n", kase, ans); else printf("Case %d: sequence nai\n", kase); } return 0;}
- uva 11536 - Smallest Sub-Array(two pointers)
- UVA 11536 - Smallest Sub-Array
- UVa 11536 - Smallest Sub-Array
- UVa 11536 Smallest Sub-Array
- Uva - 11536 - Smallest Sub-Array
- uva 11536Smallest Sub-Array
- UVA 11536 Smallest Sub-Array
- Smallest Sub-Array UVA
- 11536 - Smallest Sub-Array (two pointer)
- 【uva】11536-Smallest Sub-Array(区间移动问题)
- UVa 11536 - Smallest Sub-Array(尺取法)
- UVa 11536:Smallest Sub-Array(滑动窗口)
- uva 11536smallest sub-array(滑动窗口)
- UVA - 11536 - Smallest Sub-Array(滑动窗口)
- uva 11536 - Smallest Sub-Array(Towpointer)
- UVA - 11536 Smallest Sub-Array 纪录
- UVA:11536 Smallest Sub-Array(尺取法)
- uva 11536——Smallest Sub-Array
- 关于梦想
- sip协议中文(3)
- BlueTooth: 浅析CC2540的OSAL原理
- PAT A 1034. Head of a Gang (30)
- 30天制作操作系统之挑战指针
- uva 11536 - Smallest Sub-Array(two pointers)
- ubuntu12.04使用root登陆的简单设置
- 标准SQL语句(6) --- 数据的增删改
- JAVA动态代理
- 题目1111:单词替换
- MSSQL2008 R2使用中问题
- 定制iOS 7中的导航栏和状态栏
- 中控指纹考勤机软件登录用户名和密码忘记的解决办法
- 华为麦芒B199全焦拍照 比单反还有料