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; ...
随机推荐
- 问题集录--新手入门深度学习,选择TensorFlow 好吗?
新手入门深度学习,选择 TensorFlow 有哪些益处? 佟达:首先,对于新手来说,TensorFlow的环境配置包装得真心非常好.相较之下,安装Caffe要痛苦的多,如果还要再CUDA环境下配合O ...
- [转]jquerUI Dialog中隐藏标题栏的关闭"X"按钮
本文转自:http://blog.chinaunix.net/uid-144593-id-2804206.html 方法1. 在CSS文件中添加如下样式既可 .ui-dialog-titlebar-c ...
- ubuntu下搭建ecshop
最近在看ecmobile的开源项目,可以从http://www.ecmobile.cn/agreement.html下载源码或者从github上下载源码https://github.com/G ...
- WinForm-SuspendLayout、ResumeLayout、PerformLayou——转载
WinForm-SuspendLayout.ResumeLayout.PerformLayou——转载 https://www.cnblogs.com/si-shaohua/archive/2011/ ...
- C# 空合并运算符 ??
C#语言中,??运算符称为空合并运算符: a??b形式的空合并表达式要求a为可以为null的类型或引用类型.如果a为非null,则a??b的结果为a:否则,结果为b.仅当a为null时,该操作才计算b ...
- OC与JS交互之WKWebView
上一篇文章我们使用了JavaScriptCore框架重写了之前的示例,iOS8苹果偏爱HTML5,重构了UIWebVIew,给我们带来了WKWebView,使其性能.稳定性.功能大幅度提升,也更好的支 ...
- JSP9大内置对象
JSP9大内置对象 JSP9个内置对象:out对象 用于输出各种数据reuest对象 封装了来自客户端的各种信息response对象 封装了服务器的响应信息exception对象 封装了程序运行过程中 ...
- LeetCode SQL: Second Highest Salary
, NULL, salary) as `salary` from ( ,) tmp Write a SQL query to get the second highest salary from th ...
- Spring Data MongoDB 查询指定字段
DBObject dbObject = new BasicDBObject(); //dbObject.put("name", "zhangsan"); //查 ...
- Python实现冒泡,选择排序
def bubble(num): for i in range(len(num)-1): for j in range(len(num)-i-1): if(num[j]>num[j+1]): t ...