UVALive 5881 Unique Encryption Keys (DP)
Unique Encryption Keys
题目链接:
http://acm.hust.edu.cn/vjudge/problem/26633
Description
http://7xjob4.com1.z0.glb.clouddn.com/bced90d15c27e75270cc759651dcfa63
Input
The input contains several cipher descriptions. Each description starts with one line containing two
integer numbers M and Q separated by a space. M (1 ≤ M ≤ 1000000) is the number of encrypted
messages, Q is the number of queries (0 ≤ Q ≤ 1000000).
Each of the following M lines contains one number Ki (0 ≤ Ki ≤ 2
30) specifying the identifier of
a key used to encrypt the i-th message. The next Q lines then contain one query each. Each query is
specified by two integer numbers Bj and Ej , 1 ≤ Bj ≤ Ej ≤ M, giving the interval of messages we
want to check.
There is one empty line after each description. The input is terminated by a line containing two
zeros in place of the numbers M and Q.
Output
For each query, print one line of output. The line should contain the string “OK” if all keys used to
encrypt messages between Bj and Ej (inclusive) are mutually different (that means, they have different
identifiers). If some of the keys have been used repeatedly, print one identifier of any such key.
Print one empty line after each cipher description.
Sample Input
10 5
3
2
3
4
9
7
3
8
4
1
1 3
2 6
4 10
3 7
2 6
5 2
1
2
3
1
2
2 4
1 5
0 0
Sample Output
3
OK
4
3
OK
OK
1
Source
2016-HUST-线下组队赛-1
##题意:
给定一组序列并进行M次询问,每次询问一个区间是否有重复的数字,若有输出任意重复.
##题解:
dp[i]:区间以i为左端点时,最远能够达到并无重复数字的右端点.
对于i要找到最近的出现重复的位置,要么是i+1以后的重复,要么是num[i]本身出现重复.
转移方程:
dp[i] = min(dp[i+1], last_occ[num[i]]);
另外,感觉UVA上对着道题的特判有点问题.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define maxn 1010000
#define inf 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define IN freopen("unique.in", "r", stdin);
#define OUT freopen("unique.out", "w", stdout);
using namespace std;
int n,m;
int num[maxn];
int dp[maxn];
map<int,int> last;
int main()
{
//IN;
//OUT;
while(scanf("%d %d",&n,&m) != EOF && (m||n))
{
for(int i=1; i<=n; i++) {
scanf("%d", &num[i]);
}
last.clear();
dp[n+1] = n + 1;
for(int i=n; i>=1; i--) {
dp[i] = dp[i+1];
if(last.count(num[i])) dp[i] = min(dp[i], last[num[i]]);
last[num[i]] = i;
}
while(m--) {
int l,r; scanf("%d %d", &l,&r);
if(dp[l] > r) {
printf("OK\n");
} else {
printf("%d\n", num[dp[l]]);
}
}
printf("\n");
}
return 0;
}
UVALive 5881 Unique Encryption Keys (DP)的更多相关文章
- Codeforces VK Cup Finals #424 Div.1 A. Office Keys(DP)
显然是不可能交叉取钥匙的,于是把钥匙和人都按坐标排序就可以DP了 钥匙可以不被取,于是f[i][j]表示前i个钥匙被j个人拿的时间 f[i][j]=min(f[i-1][j],max(f[i-1][j ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)
Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
随机推荐
- 9.6. MySQL中保留字的处理
尝试使用一个识别符,例如使用嵌入式MySQL 数据类型或函数名作为表名或列名,例如TIMESTAMP 或GROUP ,会造成一个常见问题.允许你这样操作( 例如,ABS 可以作为一个列名) .但是,默 ...
- Jquery Highcharts 参数配置说明
chart: renderTo 图表的页面显示容器 defaultSeriesType 图表的显示类型(line,spline, scatter, splinearea bar,pie,area,co ...
- office编程必不可少 [转]
1. 微软官方实例: 段落.表格.图表 HOW TO:利用 Visual C# .NET 使 Word 自动新建文档 2. 学习资源 (1)Word in the Office 基础知识,必读,下面的 ...
- VC++的菜单控制和自绘菜单
菜单控制为什么即使调用EnableMenuItem菜单项后,菜单项还处于禁止状态 需要将CFrameWnd:: m_bAutomenuEnable设置为FALSE,如果该数据成员为TRUE(缺省值), ...
- memcached的LRU删除机制
1)memcached不会自动清空缓存的值如果add了一个值,但不去get它,那么这个值过期了,它也不会被清空.解释:memcached不自动检测和清空值,它只当你需要get这个值的时候,才检测这个值 ...
- UML和模式应用
引言 Applying UML and Patterns,以一个商店POS系统NextGen和一个掷骰子游戏Monopoly为例,围绕OOA/D的基本原则GRASP,以迭代作为基本方法.以UML为表达 ...
- WWDC 2015 - 概记
WWDC 2015已经过去快一个月了,今年似乎没有像去年那样变化巨大,一切都在慢慢演进,iOS.Mac OS.watchOS都变得越来越好. 新的三大平台的发布,iOS 9/Mac OS EL Cap ...
- 第三集 欠拟合与过拟合的概念、局部加权回归、logistic回归、感知器算法
课程大纲 欠拟合的概念(非正式):数据中某些非常明显的模式没有成功的被拟合出来.如图所示,更适合这组数据的应该是而不是一条直线. 过拟合的概念(非正式):算法拟合出的结果仅仅反映了所给的特定数据的特质 ...
- 【转】如何调整CHM文件中的字体!非常有爱!
原文网址:http://www.cnblogs.com/lijh_ray/archive/2011/01/25/1944668.html 如果html中字体大小是用像素px来定义,那么在IE中无法调整 ...
- 线程----BlockingQueue (转)
t java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.ut ...