题目链接

  • 题意:

    给一个长度为n的字符串,每次删除字母同样切连续的串,假设有多个,删除最左边的、最长的串。每次删除输出串的字母,每一个字母的下标(1-n)

    N (1 ≤ N ≤
    106),串仅仅包含red (‘R’),
    green (‘G’) or blue (‘B’)
  • 分析:

    题目比較麻烦,分析一下须要的任务:

    1、每次找到最长串——优先队列

    2、删除最长串后,须要合并两側的串(假设字母同样)——加Hash的链表,set(超时)

    3、每次删除须要知道这一个区间的下标都是谁——加Hash的链表,set(超时)
C++通过,G++超时……
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的更多相关文章

  1. Codeforces554 C Kyoya and Colored Balls

    C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...

  2. 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 ...

  3. 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 ...

  4. Kyoya and Colored Balls(组合数)

    Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. 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 ...

  6. 554C - Kyoya and Colored Balls

    554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...

  7. 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 ...

  8. Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

  9. 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 ...

  10. codeforces 553 A Kyoya and Colored Balls

    这个题.比赛的时候一直在往dp的方向想,可是总有一个组合数学的部分没办法求, 纯粹组合数学撸,也想不到办法-- 事实上,非常显然.. 从后往前推,把第k种颜色放在最后一个,剩下的k球.还有C(剩余的位 ...

随机推荐

  1. OpenWrt compiles

    make -r world: build failed. Please re-run make with -j1 V=s to see what's going onmake: *** [world] ...

  2. ObjectOutputStream 追加写入读取错误

    摘自http://blog.csdn.net/mitkey/article/details/50274543 问题描述: 用类ObjectOutputStream向文件写读对象时,碰到一个问题:新建一 ...

  3. Android 官方文档:(二)应用清单 —— 2.26 &lt;uses-permission&gt;标签

    syntax: <uses-permission android:name="string"         android:maxSdkVersion="inte ...

  4. tomcat建立虚拟主机

    WEB浏览器与WEBserver建立连接后,除了将请求URL中的资源路径发送给WEBserver外,还会将URL中的主机名部分作为HTTP请求消息的Host头发送给WEBserver.比如,在浏览器地 ...

  5. poj1080--Human Gene Functions(dp:LCS变形)

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17206   Accepted:  ...

  6. 那么温暖http合约,入门。

    简介 年提出.经过几年的使用与发展,得到不断地完好和扩展.眼下在WWW中使用的是HTTP/1.0的第六版,HTTP/1.1的规范化工作正在进行之中,并且HTTP-NG(Next Generation ...

  7. 跟我一起学extjs5(18--模块的新增、改动、删除操作)

    跟我一起学extjs5(18--模块的新增.改动.删除操作)         上节在Grid展示时做了一个金额单位能够手工选择的功能,假设你要增加其它功能.也仅仅要依照这个模式来操作即可了,比方说你想 ...

  8. javascript XMLHttpRequest对象全面剖析

    转载:http://www.jb51.net/article/23175.htm 一. 引言 异步JavaScript与XML(AJAX)是一个专用术语,用于实现在客户端脚本与服务器之间的数据交互过程 ...

  9. ios 项目被拒绝各种理由

    . Terms and conditions(法律与条款) 1.1 As a developer of applications for the App Store you are bound by ...

  10. 使用xib方式创建UITableViewCell,设置Label自动换行注意事项

    自定义的UITableViewCell,使用xib方式创建,想要其中的UILabel换行显示:计算Label的高度,让其自动换行,总是没有效果. 我猜测原因可能在于使用了autolayout布局.只要 ...