CodeForces - 899E Segments Removal (优先队列 + 链表)
给定一个序列,每次从序列中找一个长度最大的元素相同的片段,删除它。
如果长度相同,删除最靠左边的那个片段。
问,需要删几次。
用链表处理删除片段。对于删除之后两边又能连成一个片段的那种情况,用set记一下合并就好了。
就是如果这个片段被合并成另一片段了,那么优先队列取到这个的时候就直接pop掉。
本来用自定义的结构体实现的。看了大佬的博客,学会了pair的妙用。
pair <a, b>若被排序, a是第一关键字,b是第二关键字。
- #include <bits/stdc++.h>
- using namespace std;
- #define maxn 2000000 + 1000
- typedef pair<int, int> Node;
- int a[maxn], l[maxn], r[maxn];
- int sum[maxn], num[maxn];
- set<Node> flag;
- priority_queue<Node> q;
- int main()
- {
- int n, tot = ;
- scanf("%d", &n);
- for (int i = ; i <= n; i++) scanf("%d", &a[i]);
- int tmp = ;
- for (int i = ; i <= n; i++)
- if (a[i] == a[i+]) tmp++;
- else sum[++tot] = tmp, num[tot] = a[i], tmp = ;
- for (int i = ; i <= tot; i++)
- l[i] = i-, r[i] = i+, q.push(Node(sum[i], -i));
- int ans = ;
- while(!q.empty())
- {
- int len = q.top().first, index = -q.top().second;
- q.pop();
- if (flag.count(Node(len, -index))) continue;
- ans++;
- int left = l[index], right = r[index];
- if (left >= && right <= tot && num[left] == num[right])
- {
- flag.insert(Node(sum[left], -left));
- flag.insert(Node(sum[right], -right));
- sum[left] += sum[right];
- q.push(Node(sum[left], -left));
- r[left] = r[right];
- l[r[right]] = left;
- }
- else
- r[left] = right, l[right] = left;
- }
- printf("%d\n", ans);
- }
CodeForces - 899E Segments Removal (优先队列 + 链表)的更多相关文章
- Codeforces 899E - Segments Removal
899E - Segments Removal 思路:priority_queue+pair 代码: #include<bits/stdc++.h> using namespace std ...
- 【CodeForces】899 E. Segments Removal
[题目]E. Segments Removal [题意]给定n个数字,每次操作删除最长的连续相同数字(等长删最左),求全部删完的最少次数.n<=2*10^6,1<=ai<=10^9. ...
- Codeforces Round #452 (Div. 2) 899E E. Segments Removal
题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...
- codeforce452DIV2——E. Segments Removal
题目 Vasya has an array of integers of length n. Vasya performs the following operations on the array: ...
- Running Median POJ - 3784 (对顶堆/优先队列 | 链表)
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...
- Codeforces 681C. Heap Operations 优先队列
C. Heap Operations time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Codeforces 948C Producing Snow(优先队列+思维)
题目链接:http://codeforces.com/contest/948/problem/C 题目大意:给定长度n(n<=1e5),第一行v[i]表示表示第i堆雪的体积,第二行t[i]表示第 ...
- Codeforces Gym 101291C【优先队列】
<题目链接> 题目大意: 就是一道纯模拟题,具体模拟过程见代码. 解题分析:要掌握不同优先级的优先队列的设置.下面是对优先队列的使用操作详解: priority_queue<int& ...
- CodeForces - 799B-T-shirt buying (优先队列)
题目链接 /* Name: Copyright: Author: Date: 2018/5/2 16:09:54 Description:优先队列 */ #include <iostream&g ...
随机推荐
- ASM 磁盘组的的scrip
之前经常用如下方式进行查询:步骤 1 以oracle用户登录系统.步骤 2 执行如下命令改变ORACLE_SID环境变量.$ export ORACLE_SID=+ASM1[1或者2]需要通过ps - ...
- SpringBoot---核心---日志配置
- postgresql修改数据库名
alter database abc rename to cba;
- (转)io优化
原文:http://blog.csdn.net/gzh0222/article/details/9227393 1.系统学习 IO性能对于一个系统的影响是至关重要的.一个系统经过多项优化以后,瓶颈往往 ...
- CM5.7.2 yum离线安装笔记
一.建立yum本地服务源(yum支持http和ftp两种协议,这里使用http协议) 1.启动httpd服务 启动命令:service httpd start 关闭命令:service ht ...
- FirstAFNetWorking
// ViewController.h // FirstAFNetWorking // // Created by 张国锋 on 15/7/20. // Copyright (c) 2015年 张国锋 ...
- 关于UITableView的性能优化(历上最全面的优化分析)
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ...
- static 关键字用法
static a=0; 就是把a初始化为0:初始值为0而已 即使a是局部变量,每次进入此变量所在的函数,a值还是保持上次赋值: 在中断里建议在局部变量前加上static,以确保此变量值的寿命
- fiddler设置只抓取某一域名请求
简单易懂~
- MySQL表的碎片整理和空间回收小结
MySQL表碎片化(Table Fragmentation)的原因 关于MySQL中表碎片化(Table Fragmentation)产生的原因,简单总结一下,MySQL Engine不同,碎片化的原 ...