Codeforces 631C Report【其他】
题意:
给定序列,将前a个数进行逆序或正序排列,多次操作后,求最终得到的序列。
分析:
仔细分析可以想到j<i,且rj小于ri的操作是没有意义的,对于每个i把类似j的操作删去(这里可以用multiset或者直接模拟栈的操作),最后我们会获得一个严格下降的序列即ri>rj && i<j,并且相邻的t不相等。
那么对于ri,ri+1,对[0,ri)进行排序后,又对其子序列[0,ri+1)进行相反的排序,其实只有区间[ri+1,ri−1]]内的数是按照ti规定的排序的,并且不会再改变。所以我们只需要根据ti获取[ri+1,ri−1]]之间的值即可。
那么如何获取呢?在头尾两头设两个指针,如果对应区间要求升序,则用大的数填,否则用小的数填上。
代码:
用multiset做(做题太少第一次用set的erase)
#include <cstdio>
#include<set>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 200005;
int a[maxn], b[maxn];
typedef pair<int, int>p;
#define fi first
#define se second
multiset<p>ms;
int main (void)
{
int n, m;scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
b[i] = a[i];
}
int maxr = 0;
int r, t;
multiset<p>::iterator pos;
multiset<p>::iterator rp;
for(int i = 0; i < m; i++){
scanf("%d%d", &t, &r);
p tp = p(r, t);
maxr = max(maxr, r);
rp = ms.begin();
while(rp ->first <= tp.first && rp!=ms.end())
ms.erase(rp++);
ms.insert(tp);
}
sort(b, b + maxr);
int u = maxr - 1, l = 0;
pos = ms.end();
pos--;
int cnt = 0;
while(cnt < ms.size()){
int tmp = pos->se;
cnt++;
if(cnt == ms.size()){
for(int j = (pos->fi) - 1; j >=0; j--){
if(tmp == 1) a[j] = b[u--];
else a[j] = b[l++];
}
}
else{
rp = pos--;
for(int j = (rp->fi) - 1; j >= pos->fi; j--){
if(tmp == 1) a[j] = b[u--];
else a[j] = b[l++];
}
}
}
for(int i = 0; i < n; i++){
printf("%d%c", a[i], i == n - 1?'\n':' ' );
}
return 0;
}
模拟栈的操作
#include <cstdio>
#include<algorithm>
using namespace std;//区间递减且t交叉
const int maxn = 200005;
int a[maxn], b[maxn], t[maxn], r[maxn];
int main (void)
{
int n, m;scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
b[i] = a[i];
}
int s = 0;
for(int i = 0; i < m; i++){
scanf("%d%d", &t[i], &r[i]);
while(s > 0 && r[i] >= r[s - 1]){
s--;
}
t[s] = t[i], r[s] = r[i]; s++;
}
sort(b, b + r[0]);
int l = r[0] - 1, u = 0;
r[s] = 0;
for(int i = 1; i < s + 1; i++){
for(int j = r[i - 1] - 1; j >= r[i]; j--){
if(t[i - 1] == 2) a[j] = b[u++];
else a[j] = b[l--];
}
}
//for(int i = 0; i < n; i++) printf("%d",b[i]);
for(int i = 0; i < n; i++)
printf("%d%c", a[i], i==n-1?'\n':' ');
return 0;
}
智商捉急。。。。比赛的时候就是想不到怎么保存有意义的操作, 就断定这题是一定是要用到自己没学过的数据结构。。真的要对自己自信呀~~思考思考!!
Codeforces 631C Report【其他】的更多相关文章
- Codeforces 631C. Report 模拟
C. Report time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- codeforces 631C. Report
题目链接 按题目给出的r, 维护一个递减的数列,然后在末尾补一个0. 比如样例给出的 4 21 2 4 32 31 2 递减的数列就是3 2 0, 操作的时候, 先变[3, 2), 然后变[2, 0) ...
- codeforces 631C C. Report
C. Report time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Report CodeForces - 631C (栈)
题目链接 题目大意:给定序列, 给定若干操作, 每次操作将$[1,r]$元素升序或降序排列, 求操作完序列 首先可以发现对最后结果有影响的序列$r$一定非增, 并且是升序降序交替的 可以用单调栈维护这 ...
- CodeForces - 631C (截取法)
C. Report time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- CodeForces - 631C ——(思维题)
Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...
- CF 631C report
Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...
- Codeforces 631C
题意:给定n和m. 给定一个长度为n的序列,m次操作. 接下来m次操作,每行第一个数若为1,则增序排列,若为2则降序排列,第二个数是排列的范围,即从第一个数排序到第某个数. 思路: 首先,对于其中范围 ...
- CodeForces 631C Print Check
排序+构造+预处理 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm ...
随机推荐
- asp.net 实现treeview 选中父节点其子节点也选种中 选中子节点其父节点与根节点也被选中
1.在 Page_Load(object sender, EventArgs e) 里面加入: TreeView1.Attributes.Add("onclick", " ...
- Ubuntu 下更新或下载输入法(搜狗)
ubuntu12.04的fcitx版本不支持,不满足依赖,需要更新fcitx 添加fcitx源添加fcitx源命令 : sudo add-apt-repository ppa:fcitx-team/n ...
- Maven之——使用本地jar包并打包进war包里面的方法
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/52594602 很显然,这种方法是很不可取的,因为Maven是用来团队合作,以及发布 ...
- 通过 GCC 学习 OpenMP 框架
OpenMP 框架是使用 C.C++ 和 Fortran 进行并发编程的一种强大方法.GNU Compiler Collection (GCC) V4.4.7 支持 OpenMP 3.0 标准,而 ...
- WPF知识点--渐变色(LinearGradientBrush、GradientStop)
[LinearGradientBrush-- 使用线性渐变绘制区域](https://msdn.microsoft.com/zh-cn/library/system.windows.media.lin ...
- c++ 模板template
1.函数模板的声明 声明形式 template<typename 数据类型参数标识符> <返回类型><函数名>(参数表) { 函数体 } 注: templa ...
- Mysql使用导出导入数据库
要在两台不同的电脑上进行开发,数据库需要统一,由于自己第一次完整的设计表结构,因此多次更改表结构,造成了很多不必要的麻烦, 需要将数据库导出成sql脚本: 命令行下具体用法如下: mysqldump ...
- php基础查找算法
1.顺序查找 function line_search($array,$tar) { if(!is_array($array) || count($array) < 1) return fals ...
- 2.10.3 nav 元素
nav <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...
- ZOJ - 3204 Connect them 最小生成树
Connect them ZOJ - 3204 You have n computers numbered from 1 to n and you want to connect them to ma ...