【UOJ】【UR #2】猪猪侠再战括号序列(splay/贪心)
纪念伟大的没有调出来的splay。。。
竟然那个find那里写错了!!!!!!!!!!!!!
以后要记住:一定要好好想过!
(正解的话我就不写了,太简单了。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=200005;
struct node *null;
struct node {
node *c[2], *f;
int s, k, sum;
bool rev;
node(int _k=0) { c[0]=c[1]=f=null; s=1; k=sum=_k; rev=0; }
void pushup() { s=1+c[0]->s+c[1]->s; sum=k+c[0]->sum+c[1]->sum; }
void setc(node *x, bool d) { c[d]=x; x->f=this; }
bool d() { return f->c[1]==this; }
void upd() {
if(this==null) return;
rev=!rev;
swap(c[0], c[1]);
}
void pushdown() {
if(rev) {
rev=0;
c[0]->upd();
c[1]->upd();
}
}
}*root;
void rot(node *x) {
node *f=x->f;
f->pushdown(); x->pushdown(); bool d=x->d();
f->f->setc(x, f->d());
f->setc(x->c[!d], d);
x->setc(f, !d);
f->pushup();
if(f==root) root=x;
}
void splay(node *x, node *f=null) {
x->pushdown();
while(x->f!=f) {
if(x->f->f==f) rot(x);
else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
}
x->pushup();
}
void Pr(node *x) {
if(x==null) return;
Pr(x->c[0]);
printf("%c", x->k?'(':')');
Pr(x->c[1]);
}
void P(node *x=root) {
Pr(x); puts("");
}
void init() { null=new node; null->s=0; null->c[0]=null->c[1]=null->f=null; root=null; }
void build(char *s, int l, int r, node *&x) {
if(l>r) return;
int mid=(l+r)>>1;
x=new node(s[mid]=='(');
if(l==r) return;
build(s, l, mid-1, x->c[0]);
build(s, mid+1, r, x->c[1]);
if(l<=mid-1) x->c[0]->f=x;
if(mid+1<=r) x->c[1]->f=x;
x->pushup();
}
void build(char *s) {
int len=strlen(s);
node *x=null;
root=new node();
x=new node();
root->setc(x, 1);
x=new node();
build(s-1, 1, len, x);
root->c[1]->setc(x, 0);
root->c[1]->pushup();
root->pushup();
}
node* find(node *x) {
if(x==null) return null;
x->pushdown();
if(x->c[0]->sum==0 && x->k) return x; //这里啊啊啊
if(x->c[0]->sum) return find(x->c[0]);
return find(x->c[1]);
}
node* sel(node *x, int k) {
if(x==null) return null;
x->pushdown();
int s=x->c[0]->s;
if(s==k) return x;
if(s<k) return sel(x->c[1], k-s-1);
else return sel(x->c[0], k);
}
node* getrange(int l, int r) {
splay(sel(root, l-1));
splay(sel(root, r+1), root);
return root->c[1]->c[0];
}
int getpos(int l, int r) {
node *x=getrange(l, r);
x=find(x); //dbg(x->k);
splay(x);
return x->c[0]->s;
}
void fix(int l, int r) {
node *x=getrange(l, r); //P(x);
x->upd();
root->c[1]->pushup();
root->pushup();
}
int getinf(int l) {
node *r=getrange(l, l);
return r->k;
}
char s[N];
int ans;
int x[N], y[N], n, len;
// int ifind(int now) {
// int ret=now;
// while(ret<=len) if(s[ret]=='(') return ret; else ++ret;
// return 0;
// }
// void fix(int l, int r) {
// while(l<r) swap(s[l], s[r]), ++l, --r;
// }
int main() {
scanf("%s", s+1);
init();
len=strlen(s+1);
//dbg(s+1);
n=len>>1;
build(s+1);
//dbg(getpos(2, len));
//fix(1, getpos(2, len));
//node *xx=sel(root, 1); dbg(xx->s); P();
for1(i, 1, n) {
//printf("%c\n", getinf(i)?'(':')');
if(getinf(i)!=1) {
++ans;
int pos=getpos(i+1, len);
if(!pos) { puts("error"); return 0; }
x[ans]=i;
y[ans]=pos;
fix(i, pos);
//dbg(s+1);
}
//P();
}
printf("%d\n", ans);
for1(i, 1, ans) {
printf("%d %d\n", x[i], y[i]);
}
return 0;
}
【UOJ】【UR #2】猪猪侠再战括号序列(splay/贪心)的更多相关文章
- uoj problem 31 猪猪侠再战括号序列
题目大意: 给定一个长度为2n的括号序列.定义一个关于区间[l,r]的翻转操作为位置平移对调. 即翻转")))()("可以得到"()()))((" 用不超过n次 ...
- uoj #31. 【UR #2】猪猪侠再战括号序列 贪心
#31. [UR #2]猪猪侠再战括号序列 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/31 Descript ...
- 学长小清新题表之UOJ 31.猪猪侠再战括号序列
学长小清新题表之UOJ 31.猪猪侠再战括号序列 题目描述 大家好我是来自百度贴吧的_叫我猪猪侠,英文名叫\(\_CallMeGGBond\). 我不曾上过大学,但这不影响我对离散数学.复杂性分析等领 ...
- UOJ#31 【UR #2】猪猪侠再战括号序列
传送门http://uoj.ac/problem/31 大家好我是来自百度贴吧的_叫我猪猪侠,英文名叫_CallMeGGBond. 我不曾上过大学,但这不影响我对离散数学.复杂性分析等领域的兴趣:尤其 ...
- 【UR #2】猪猪侠再战括号序列
UOJ小清新题表 题目摘要 UOJ链接 有一个由 \(n\) 个左括号 "(" 和 \(n\) 个右括号 ")" 组成的序列.每次操作时可以选定两个数 \(l, ...
- 【UR #2】猪猪侠再战括号序列 题解
题目链接 前言 是的没脑子选手只会做签到题. 思路分析 一开始把题目看成反转括号的状态,直接浪费 \(40\ mins\) . 我们考虑把不确定的"正确括号"转换成一个固定的括号序 ...
- 【BZOJ-2329&2209】括号修复&括号序列 Splay
2329: [HNOI2011]括号修复 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 1007 Solved: 476[Submit][Statu ...
- bzoj 2209: [Jsoi2011]括号序列 splay
2209: [Jsoi2011]括号序列 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 833 Solved: 392[Submit][Status ...
- BZOJ 2209: [Jsoi2011]括号序列 [splay 括号]
2209: [Jsoi2011]括号序列 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1111 Solved: 541[Submit][Statu ...
随机推荐
- 配置caffe的python环境时make pycaffe提示fatal error: numpy/arrayobject.h No such file or directory解决方法
重装numpy: sudo pip uninstall numpy sudo pip install numpy 是没有用的... 解决的办法就是: sudo apt-get install pyth ...
- POJ 1797 Heavy Transportation (Dijkstra变形)
F - Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- Textview下划线注册用户跳转实现
在xml中: <TextView android:id="@+id/textView_regtext" android:layout_width="wrap_con ...
- Metasploit是一款开源的安全漏洞检测工具,
Metasploit是一款开源的安全漏洞检测工具,可以帮助安全和IT专业人士识别安全性问题,验证漏洞的缓解措施,并管理专家驱动的安全性进行评估,适合于需要核实漏洞的安全专家,同时也适合于强大进攻能力的 ...
- systemd在各个linux发行版的普及
后面我要说下自己的意见: 原则如果阻碍了进步,那还算个屁,不客气地说,UNIX 原则已经过时了. 移植性问题:我除了 Mac 外不用任何 BSD 系统,当然 Mac 上一般只做开发不做运维(但就算如此 ...
- C/C++程序终止时执行的函数——atexit()函数详解
很多时候我们需要在程序退出的时候做一些诸如释放资源的操作,但程序退出的方式有很多种,比如main()函数运行结束.在程序的某个地方用exit()结束程序.用户通过Ctrl+C或Ctrl+break操作 ...
- codeforces A. Group of Students 解题报告
题目链接:http://codeforces.com/problemset/problem/357/A 题目意思:将一堆人分成两组:beginners 和 intermediate coders .每 ...
- 基因变异(codevs 3194)
题目描述 Description 小毛终于来到了冥王星,这是一颗已经不属于行星的矮行星,它的表面温度低于-220度.在这里,小毛惊奇的发现,他带来的厌氧菌开始了基因变异,裂变的速度与光照时间(秒)成乘 ...
- 二、JavaScript语言--JS基础--JavaScript进阶篇--数组
1.什么事数组 我们知道变量用来存储数据,一个变量只能存储一个内容.假设你想存储10个人的姓名或者存储20个人的数学成绩,就需要10个或20个变量来存储,如果需要存储更多数据,那就会变的更麻烦.我们用 ...
- MVC学习笔记---MVC生命周期及管道
ASP.NET和ASP.NET MVC的HttpApplication请求处理管道有共同的部分和不同之处,本系列将体验ASP.NET MVC请求处理管道生命周期的19个关键环节. ①以IIS6.0为例 ...