题目链接:

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---优先队列+链表(用数组模拟)的更多相关文章

  1. 1350: To Add Which? (优先队列+贪心 或者 数组模拟)

    1350: To Add Which? Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitt ...

  2. POJ 1330 Tarjan LCA、ST表(其实可以数组模拟)

    题意:给你一棵树,求两个点的最近公共祖先. 思路:因为只有一组询问,直接数组模拟好了. (写得比较乱) 原题请戳这里 #include <cstdio> #include <bits ...

  3. 【POJ 3476】A Game with Colored Balls

    POJ 3476 首先写了个treap,然后常数太大tle了... 然后想了个极为复杂的方法,是一共7个dsu,3个bit,还有一个set.然后写了一半就歇菜了... 然后看dxm的方法,是这样做的: ...

  4. Codeforces554 C Kyoya and Colored Balls

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

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

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

  7. Kyoya and Colored Balls(组合数)

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

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

  9. 554C - Kyoya and Colored Balls

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

随机推荐

  1. C语言位运算、移位运算 经典示例

    概述: C语言的位级运算可以运用到任何“整数”的数据类型上,如char.short.int.long.long long.或者unsigned这样的限定词.基本的位运算有与.或.非.异或等等. C语言 ...

  2. guava快速入门(三)

    Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support] ...

  3. 怎么用PHP发送HTTP请求(POST请求、GET请求)?

    file_get_contents版本: 01 /** 02 * 发送post请求 03 * @param string $url 请求地址 04 * @param array $post_data ...

  4. HotSpot 虚拟机中对象的创建过程

  5. mqtt server搭建和web中使用js-sdk订阅发布消息

    1.mqtt server搭建(From:https://www.cnblogs.com/huhongy/p/7929299.html) window安装MQTT服务器,我这里下载了一个apache- ...

  6. Android学习笔记(4)----Rendering Problems(The graphics preview in the layout editor may not be accurate)

    在Android Studio中新建了一个 setting.xml 文件,布局好文件后,从 Text 界面切换到 Design 界面,显示了如下错误: 网上搜寻 The graphics previe ...

  7. 从零开始安装、编译、部署 Docker

    简介 主要介绍如何从基础系统debian部署docker关于docker基础知识在 相关资料 里有链接 安装docker 1.使用root用户身份添加apt源添加public key使docker的安 ...

  8. Android ListView实现新闻客户端的新闻内容图文混排

    布局文件: <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/andro ...

  9. jpa dialect设置

    1.如果配置文件格式为application.properties,在配置文件中添加以下代码即可: spring.jpa.database-platform=org.hibernate.dialect ...

  10. ORM注意点

    add:是追加 set:是覆盖