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; ...
随机推荐
- 禁止选中页面内容-兼容ie、firefox、chrome
使用js禁止用户选中网页上的内容,IE及Chrome下的方法一样.使用onselectstart, 比如: 在body中加入<body onselectstart="return fa ...
- linux的日常经常使用的命令
现在经常用到linux命令,又时候回忘记,我就做个小笔记,大家也可以补充补充.....可以评论一下,我会截图做笔记的 netstat -ntlp //查看当前系统进程和端口等信息 tail -f fi ...
- jdk锁相关
锁类型 可重入锁:在执行对象中所有同步方法不用再次获得锁 可中断锁:在等待获取锁过程中可中断 公平锁: 按等待获取锁的线程的等待时间进行获取,等待时间长的具有优先获取锁权利 读写锁:对资源读取和写入的 ...
- 在 Ubuntu上使用 MySQL
MySQL 安装配置 https://help.ubuntu.com/12.04/serverguide/mysql.html MySQL Manual http://dev.mysql.com/do ...
- BZOJ P1212 [HNOI2004] L语言
标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的集合. 我 ...
- BZOJ4516: [Sdoi2016]生成魔咒(后缀数组 set RMQ)
题意 题目链接 Sol 毒瘤SDOI 终于有一道我会做的题啦qwq 首先,本质不同的子串的个数 $ = \frac{n(n + 1)}{2} - \sum height[i]$ 把原串翻转过来,每次就 ...
- 03--CSS布局设置
一 盒模型 盒模型 在CSS中,"box model"这一术语是用来设计和布局时使用,然后在网页中基本上都会显示一些方方正正的盒子.我们称为这种盒子叫盒模型. 盒模型有两种:标准模 ...
- 使用PuTTy在CentOS下安装web.py与简单的文件传输
两周前,出于帮朋友忙的目的,尝试了一下微信公众号的菜单自定义与自动回复功能的实现,成了. 两周后,需要将代码转移至朋友新购的服务器上,发现基本操作全忘记了,麻瓜!所以记一笔,希望也能对大家也有帮助. ...
- spring 与springmvc容器的关系
spring容器是springmvc的父容器,而父容器是不能访问子容器中的东西,但子容器可以访问父容器的东西
- Linux 的su 与sudo 的区别,查看所有用户
首先,我们要知道系统当中存在哪些用户. 1.用户名和密码的存储位置 存储帐号的文件:/etc/passwd 存储密码的文件:/etc/shadow 通过/etc/shadow获取的只是密码加密后的Ha ...