POJ - 3476 A Game with Colored Balls---优先队列+链表(用数组模拟)
题目链接:
https://cn.vjudge.net/problem/POJ-3476
题目大意:
一串长度为N的彩球,编号为1-N,每个球的颜色为R,G,B,给出它们的颜色,然后进行如下操作:
每次消除连续颜色最长的最左端的一串,剩下的球如果分成两串,就把左右两串连接起来,输出每次消除的球的颜色及编号。
解题思路:
将球的同一颜色的串压入优先队列中,每次取出最长的串,相同长度的串取最左端的串。
取出来之后,如果将小球分成了两串,如果两端颜色一样可以合并,那就网优先队列中压入新合成的串。每次取出串之后,将串的每一位进行标记,原因是由于没有将原来的两串删除就直接直接加入合并后的串,所以只需要标记一下已经取出,那么后加入的串由于长度长会先出优先队列。算法是正确的。
对于需要输出具体是那些小球,利用pre数组和next数组,pre[i]表示第i个小球前面连着的小球的下标。next则表示后面连着的小球,用这两个数组模拟双端链表,可以输出具体是那些小球。
用C++提交不会超时
//#include<bits/stdc++.h>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 1e6 + ;
typedef long long ll;
struct node
{
char c;
int pos, len;
node(char c, int pos, int len):c(c), pos(pos), len(len){}
bool operator <(const node& a)const
{
return len < a.len || len == a.len && pos > a.pos;//优先队列
}
};
char s[maxn];
int pre[maxn], next[maxn];
bool vis[maxn];
priority_queue<node>q;
int main()
{
scanf("%s", s);
int n = strlen(s);
for(int i = ; i < n;)
{
int st = i, len = ;
while(s[++i] == s[st])len++;
//cout<<s[st]<<" "<<st<<" "<<len<<endl;
q.push(node(s[st], st, len));
}
for(int i = ; i < n; i++)
{
pre[i] = i - , next[i] = i + ;
}
memset(vis, , sizeof(vis));
while(!q.empty())
{
node now = q.top();
q.pop();
if(now.len <= )break;
if(vis[now.pos])continue;
printf("%c", now.c);
int head = pre[now.pos], tail = now.pos;
for(int i = ; i < now.len; i++, tail = next[tail])
{
vis[tail] = ;
printf(" %d", tail + );
}
puts("");
if(head >= )next[head] = tail;
if(tail < n)pre[tail] = head;
if(head < || tail >= n || s[head] != s[tail])continue; int len = ;
while(pre[head] >= && s[pre[head]] == s[head])
head = pre[head], len++;
while(next[tail] < n && s[next[tail]] == s[tail])
tail = next[tail], len++;
q.push(node(s[head], head, len));
}
return ;
}
POJ - 3476 A Game with Colored Balls---优先队列+链表(用数组模拟)的更多相关文章
- 1350: To Add Which? (优先队列+贪心 或者 数组模拟)
1350: To Add Which? Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitt ...
- POJ 1330 Tarjan LCA、ST表(其实可以数组模拟)
题意:给你一棵树,求两个点的最近公共祖先. 思路:因为只有一组询问,直接数组模拟好了. (写得比较乱) 原题请戳这里 #include <cstdio> #include <bits ...
- 【POJ 3476】A Game with Colored Balls
POJ 3476 首先写了个treap,然后常数太大tle了... 然后想了个极为复杂的方法,是一共7个dsu,3个bit,还有一个set.然后写了一半就歇菜了... 然后看dxm的方法,是这样做的: ...
- 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; ...
随机推荐
- Spring----有关bean的配置
1.单例类的配置如果我们想创建一个单例类的bean,只能会通过静态工厂来创建.下图为一个单例类: Stage并没有提供公开的构造方法,构造方法都是私有的,必须通过getInstance()方法获得已经 ...
- PHP高级工程师面试 - 笔试题
Part1:HTTP协议 1.状态码的含义 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求 ...
- Vim 技巧
:r 文件名 导入另一文件到当前文件中 :! 命令 可以不退出当前编辑的文本而能执行系统的命令 自定义快捷键 注意这里的^P这个是ctrl + V + P :map ^P I//<ESC> ...
- mongdb启动报错
2018-08-19T12:25:31.707+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1 ...
- spring底层原理解析
注解测试:如何使用注解(去掉配置文件)开发 新建MainConfig类 注解测试:新建MainTest2注解测试,用来测试//AnnoatationConfigApplicationContext: ...
- Foxmail邮件收取网易企业邮件配置
- BZOJ1093 [SCOI2003]字符串折叠
Description 折叠的定义如下: 1. 一个字符串可以看成它自身的折叠.记作S S 2. X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) SSSS…S(X个S). ...
- 配置文件出错 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): <!-- mybatis 配置- ...
- ORM注意点
add:是追加 set:是覆盖
- github连接提示
解决办法: 1,将连接方式从http更换为ssh.注意,github.com后面一定有(冒号): git remote rm origin git remote add origingit@githu ...