codeforces 45C C. Dancing Lessons STL
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
The first line contains an integer n (1 ≤ n ≤ 2·105) — the number of people. The next line contains n symbols B or G without spaces. Bstands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the dancing skill. People are specified from left to right in the order in which they lined up.
Print the resulting number of couples k. Then print k lines containing two numerals each — the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
4
BGBG
4 2 4 3
2
3 4
1 2
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e6+, M = 1e6, mod = 1e9+, inf = 2e9; set< pair < int, pii > > s;
set< pair < int, pii > >:: iterator it;
vector< pii > ans;
int n,v[N],nex[N],last[N];
char a[N];
bool ok[N];
int main() {
scanf("%d%s",&n,a+);
for(int i = ; i <= n; ++i) scanf("%d",&v[i]);
for(int i = ; i <= n; ++i) nex[i] = i+, last[i] = i - ,ok[i] = true;
for(int i = ; i < n; ++i) {
if(a[i]^a[i+])
s.insert(MP(abs(v[i]-v[i+]),MP(i,i+)));
}
while(s.size()) {
it = s.begin();
int bef = (*it).second.first;
int blc = (*it).second.second;
s.erase(it);
if(ok[bef] && ok[blc])
ans.push_back(MP(bef,blc)),
ok[bef] = ok[blc] = false;
else {
continue;
}
int befs = last[bef];
int blcs = nex[blc];
nex[befs] = blcs;
last[blcs] = befs;
if(befs <= n && befs >= && blcs <= n && blcs >= && a[befs] ^ a[blcs]) {
s.insert(MP(abs(v[befs]-v[blcs]),MP(befs,blcs)));
}
}
cout<<ans.size()<<endl;
for(int i = ; i < ans.size(); ++i) cout<<ans[i].first<<" "<<ans[i].second<<endl;
return ;
}
codeforces 45C C. Dancing Lessons STL的更多相关文章
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- Codeforces 915E Physical Education Lessons
原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...
- codeforces 893F - Physical Education Lessons 动态开点线段树合并
https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...
- Codeforces 915E. Physical Education Lessons(动态开点线段树)
E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...
- CodeForces 1000A Codehorses T-shirts(STL map、思维)
https://codeforces.com/problemset/problem/1000/A 题意: 有n个人,给出每个人的衣服的尺码,现在,将这n件衣服的尺码换成另外的n种尺码,如果有尺码一样的 ...
- CodeForces 993B Open Communication(STL 模拟)
https://codeforces.com/problemset/problem/993/b 这题不难,暴力就能过,主要是题意太难懂了 题意: 现在有两个人,每个人手中有一对数,第一个人手中的数是n ...
- codeforces 915E - Physical Education Lessons 动态开点线段树
题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^7$会 ...
- T-shirt buying CodeForces - 799B (小根堆+STL)
题目链接 思路: 由于题目说了只有1,2,3,三种色号的衣服,然后开三个对应色号的小根堆, 我是根据pair<int,int> 创建了一个以价格小的优先的优先队列. pair中的另外一个i ...
- CodeForces 557C Arthur and Table STL的使用
题意:一个桌子有n条腿,每条腿有一定的长度l,和砍下的花费w,现在规定,桌子稳的条件是长度最长的腿(可多个)的数量大于长度小于它的桌子腿数量,且不存在比他还长的桌子腿,求让桌子腿稳定的最小的花费 #i ...
随机推荐
- div加jquery实现iframe标签的功能
由于最近项目需要,前端后台都完全采用div+css的方式布局.因而左思右想,有什么办法可以替代常用的iframe上下左右的布局方式,而且页面只是局部刷新.参考了许多前辈的资料,并加以整理,因而有了以下 ...
- Unity3d 查找所选的是否引用过某资源
一.使用方式: 1.选择要被查找的资源,右键->Find Reference 2.把资源拽入Res,点Find 3.输出结果见Console //代码 using UnityEngine; us ...
- winrt获取文件MD5码
//小文件 public static string ComputeMD5(byte[] bytes) { var alg = HashAlgorithmProvider.OpenAlgorithm( ...
- [转]Android 学习资料分享(2015 版)
转 Android 学习资料分享(2015 版) 原文地址:http://www.jianshu.com/p/874ff12a4c01 目录[-] 我是如何自学Android,资料分享(2015 版) ...
- nyoj19_排列
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...
- 解决At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this log
pom增加:<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</art ...
- 手工加载DLL
1.为了能找到dll的函数地址,生成dll时需要将其中的函数声明为C函数(extern "C"): #ifndef __MYDLL_H#define __MYDLL_H #ifde ...
- Ruby on Rails 接口无法调试的问题
1. 客户端 ip 与服务器 ip需要在同一网段 2. 如果 rails 版本是4.2及以上需要通过 rails s -b 0.0.0.0启动 3. 跑服务的mac需要关闭防火墙
- 求sqrt()底层效率问题(二分/牛顿迭代)
偶然看见一段求根的神代码,于是就有了这篇博客: 对于求根问题,通常我们可以调用sqrt库函数,不过知其然需知其所以然,我们看一下求根的方法: 比较简单方法就是二分咯: 代码: #include< ...
- Java动态代理一Proxy
什么是动态代理? 动态代理可以提供对另一个对象的访问,同时隐藏实际对象的具体事实.代理一般会实现它所表示的实际对象的接口.代理可以访问实际对象,但是延迟实现实际对象的部分功能,实际对象实现系统的实际功 ...