A Game with Colored Balls
- 题意:
给一个长度为n的字符串,每次删除字母同样切连续的串,假设有多个,删除最左边的、最长的串。每次删除输出串的字母,每一个字母的下标(1-n)
N (1 ≤ N ≤
106),串仅仅包含red (‘R’),
green (‘G’) or blue (‘B’) - 分析:
题目比較麻烦,分析一下须要的任务:
1、每次找到最长串——优先队列
2、删除最长串后,须要合并两側的串(假设字母同样)——加Hash的链表,set(超时)
3、每次删除须要知道这一个区间的下标都是谁——加Hash的链表,set(超时)
const int MAXN = 1100000; struct Node
{
int pos, len;
char val;
Node *nxt, *pre;
Node (int p = 0, int n = 0, char v = 0) : pos(p), len(n), val(v) {}
bool operator< (const Node& rhs) const
{
return pos < rhs.pos;
}
void erase()
{
pre->nxt = nxt;
nxt->pre = pre;
}
} nd[MAXN], pt, fst, lst;
int tot; struct HeapNode
{
int val, pos;
HeapNode (int v = 0, int p = 0) : val(v), pos(p) {}
bool operator< (const HeapNode& rhs) const
{
if (val != rhs.val)
return val < rhs.val;
return pos > rhs.pos;
}
} vt; priority_queue<HeapNode> q;
char ipt[MAXN];
bool vis[MAXN];
Node* to[MAXN], *pit, *pl, *pr;
int nxt[MAXN], pre[MAXN]; void init(int n)
{
REP(i, n)
{
nxt[i] = i + 1;
if (i)
pre[i] = i - 1;
}
}
void erase(int l, int r)
{
int p = pre[l], n = nxt[r];
nxt[p] = n;
pre[n] = p;
} int main()
{
while (~RS(ipt))
{
fst.val = -1; fst.nxt = &lst; fst.pre = NULL;
lst.val = -2; lst.pre = &fst; lst.nxt = NULL;
CLR(vis, false);
while (!q.empty())
q.pop();
tot = 0;
int len = strlen(ipt);
init(len + 2); nd[tot++] = Node(1, 1, ipt[0]);
FF(i, 1, len)
{
if (ipt[i] == nd[tot - 1].val)
nd[tot - 1].len++;
else
{
nd[tot].pos = i + 1;
nd[tot].len = 1;
nd[tot].val = ipt[i];
tot++;
}
}
fst.nxt = &nd[0]; nd[0].pre = &fst;
lst.pre = &nd[tot - 1]; nd[tot - 1].nxt = &lst;
REP(i, tot)
{
if (i != 0)
nd[i].pre = &nd[i - 1];
if (i != tot - 1)
nd[i].nxt = &nd[i + 1];
to[nd[i].pos] = &nd[i];
q.push(HeapNode(nd[i].len, nd[i].pos));
}
while (!q.empty())
{
vt = q.top();
q.pop();
if (vt.val == 1)
break;
if (vis[vt.pos])
continue;
pt.pos = vt.pos;
pit = to[vt.pos]; int idx = vt.pos;
printf("%c", ipt[vt.pos - 1]);
REP(i, vt.val)
{
printf(" %d", idx);
erase(idx, idx);
idx = nxt[pre[idx]];
}
puts(""); pl = pit->pre; pr = pit->nxt;
if (pl->val == pr->val)
{
pl->len += pr->len; vis[pr->pos] = true;
pr->erase(); q.push(HeapNode(pl->len, pl->pos));
}
vis[vt.pos] = true;
pit->erase();
}
}
return 0;
}
A Game with Colored Balls的更多相关文章
- Codeforces554 C Kyoya and Colored Balls
C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- codeforces 553A . Kyoya and Colored Balls 组合数学
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合
C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Kyoya and Colored Balls(组合数)
Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))
C. Kyoya and Colored Balls Kyoya Ootori has a bag with n colored balls that are colored with k diffe ...
- 554C - Kyoya and Colored Balls
554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
- Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
- codeforces 553A A. Kyoya and Colored Balls(组合数学+dp)
题目链接: A. Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes i ...
- codeforces 553 A Kyoya and Colored Balls
这个题.比赛的时候一直在往dp的方向想,可是总有一个组合数学的部分没办法求, 纯粹组合数学撸,也想不到办法-- 事实上,非常显然.. 从后往前推,把第k种颜色放在最后一个,剩下的k球.还有C(剩余的位 ...
随机推荐
- HDU 1272 小希的迷宫(并查集) 分类: 并查集 2015-07-07 23:38 2人阅读 评论(0) 收藏
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
- mongoDb c driver
1,yum dependencies Centos,RHEL Fedora: $ sudo yum install git gcc automake autoconf libtool Debian: ...
- linux 下apache安装、启动和配置
linux 下 apache安装 1:系统安装,这里就不说了,网上有很多,也很简单.顺便说下,我用的是redhat 9: 2:在图形界面下下载apache 安装包,我下的是 httpd-2.2.9.t ...
- Segment(技巧 相乘转换成相加 + java)
Segment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- APPCAN学习笔记004---AppCan与Hybrid,appcan概述
APPCAN学习笔记004---AppCan与Hybrid,appcan概述 技术qq交流群:JavaDream:251572072 本节讲了appcan的开发流程,和开发工具 笔记不做具体介绍了,以 ...
- c# 高斯模糊
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using Syste ...
- RMAN数据库恢复之恢复归档日志文件
恢复归档日志文件如果只是为了在恢复数据文件之后应用归档文件,那并不需要手动对归档文件进行恢复,RMAN会在RECOVER时自动对适当的归档进行恢复.单独恢复归档文件一般是有特别的需求,如创建了Data ...
- 【输入输出挂】【Uva11462】Age Sort
例题17 年龄排序(Age Sort, UVa 11462)照从小到大的顺序输出. [输入格式] 输入包含多组测试数据.每组数据的第一行为整数n(0<n≤2 000 000),即居民总数:下一 ...
- Git使用过程
Git-------目前世界上最先进的分布式版本控制系统(没有之一) 什么是版本控制系统? 说简单点,就是一个文件,对其增加.删除.修改都可以被记录下来,不仅自己可以修改,其他人也可以进行修改 每次对 ...
- C#实现WinForm传值实例解析
C#实现WinForm传值的问题经常会做为公司面试的题目,那么作为学习C#以及WinForm传值,我们需要掌握哪些方法和思路呢?下面我们就向你介绍详细的思路和实现的具体步骤,希望对你有所帮助. C#实 ...