「LibreOJ β Round #2」DP 一般看规律
来源:互联网 发布:诲女知之乎 读法 编辑:程序博客网 时间:2024/05/20 21:18
题目链接
Description
给定一个长度为
每次操作的内容为:给定
同时我们有一份代码:
int ans = 2147483647;for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (a[i] == a[j]) ans = std::min(ans, j - i); }}std::cout << ans << std::endl;
请在每次修改后输出代码运行的结果。
Input
第一行两个数,表示
第二行
然后
Output
对于每次修改,输出答案。
Sample Input
5 10
2 7 6 3 8
6 1
7 1
1 3
5 6
1 7
9 5
1 10
7 6
7 5
3 9
Sample Output
2147483647
1
1
1
1
1
1
1
1
1
Hint
每个出现的数字绝对值在 int 范围内。
题目大意
给出操作,将序列中所有一个数字替换为另一个,询问每次操作后距离最近的两个相同数字的距离。
题解
每个数字只与他的前驱和后继产生贡献。构建
#include<bits/stdc++.h>using namespace std;map< int, set<int> > a;int n, m, ans = INT_MAX;void update(int x, int y){ set<int>::iterator it; it = a[x].lower_bound(y); if(it != a[x].end()) ans = min(ans, *it - y); if(it != a[x].begin()) it--, ans = min(ans, y - *it);}void init(){ scanf("%d%d", &n, &m); for(int i = 1, x; i <= n; i++){ scanf("%d", &x); update(x, i); a[x].insert(i); }}void work(){ for(int i = 1, x1, x2; i <= m; i++){ scanf("%d%d", &x1, &x2); if(x1 == x2){ printf("%d\n", ans); continue; } if(a[x1].size() > a[x2].size()) swap(a[x1], a[x2]); for(set<int>::iterator it = a[x1].begin(); it != a[x1].end(); it++){ update(x2, *it); a[x2].insert(*it); } a[x1].clear(); printf("%d\n", ans); }}int main(){ init(); work(); return 0;}
阅读全文
0 0
- 「LibreOJ β Round #2」DP 一般看规律
- 516. 「LibreOJ β Round #2」DP 一般看规律 [set/SPLAY] 【STL/数据结构】
- #516. 「LibreOJ β Round #2」DP 一般看规律 set启发式合并
- #516. 「LibreOJ β Round #2」DP 一般看规律 stl容器
- [启发式合并]#516. 「LibreOJ β Round #2」DP 一般看规律
- 【并查集+平衡树启发式合并】LibreOJ β Round #2[DP一般看规律]题解
- #515. 「LibreOJ β Round #2」贪心只能过样例 (简单dp+bitset优化)
- #507. 「LibreOJ NOI Round #1」接竹竿 dp
- 【LibreOJ】6177 「美团 CodeM 初赛 Round B」送外卖2 状压DP
- [状压DP]LibreOJ #6177. 「美团 CodeM 初赛 Round B」送外卖2 题解
- LibreOJ #505.「LibreOJ β Round」ZQC 的游戏 网络流
- LibreOJ #504. 「LibreOJ β Round」ZQC 的手办 线段树+堆
- [结论] LibreOJ #520. 「LibreOJ β Round #3」绯色 IOI(开端)
- [匹配+拓扑] LibreOJ #521. 「LibreOJ β Round #3」绯色 IOI(抵达)
- [数学杂题] LibreOJ #530「LibreOJ β Round #5」最小倍数
- [数学杂题] LibreOJ#532. 「LibreOJ β Round #5」随机数列
- loj#526. 「LibreOJ β Round #4」子集
- 「LibreOJ β Round #2」贪心只能过样例 [bitset]【STL】
- osg::NotifyHandler
- CSS三大特性继承性,层叠性,优先级
- java种前后台线程和进程的关系
- 设置nv12的矩形边框
- 扫描软Agisoft.PhotoScan.Professional.v1.3.2.4164 x64
- 「LibreOJ β Round #2」DP 一般看规律
- 2017上海市高校程序设计邀请赛_L
- MediaPlayer
- 2017.7.4
- java代理
- Java模板方法模式
- nginx启动错误
- 读Excel代码示例
- BZOJ 2463 [中山市选2009]谁能赢呢?