题目链接:

https://cn.vjudge.net/problem/POJ-3476

题目大意:

一串长度为N的彩球,编号为1-N,每个球的颜色为R,G,B,给出它们的颜色,然后进行如下操作:

每次消除连续颜色最长的最左端的一串,剩下的球如果分成两串,就把左右两串连接起来,输出每次消除的球的颜色及编号。

解题思路:

将球的同一颜色的串压入优先队列中,每次取出最长的串,相同长度的串取最左端的串。

取出来之后,如果将小球分成了两串,如果两端颜色一样可以合并,那就网优先队列中压入新合成的串。每次取出串之后,将串的每一位进行标记,原因是由于没有将原来的两串删除就直接直接加入合并后的串,所以只需要标记一下已经取出,那么后加入的串由于长度长会先出优先队列。算法是正确的。

对于需要输出具体是那些小球,利用pre数组和next数组,pre[i]表示第i个小球前面连着的小球的下标。next则表示后面连着的小球,用这两个数组模拟双端链表,可以输出具体是那些小球。

用C++提交不会超时

 1 //#include<bits/stdc++.h>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxn = 1e6 + 10;
 7 typedef long long ll;
 8 struct node
 9 {
10     char c;
11     int pos, len;
12     node(char c, int pos, int len):c(c), pos(pos), len(len){}
13     bool operator <(const node& a)const
14     {
15         return len < a.len || len == a.len && pos > a.pos;//优先队列
16     }
17 };
18 char s[maxn];
19 int pre[maxn], next[maxn];
20 bool vis[maxn];
21 priority_queue<node>q;
22 int main()
23 {
24     scanf("%s", s);
25     int n = strlen(s);
26     for(int i = 0; i < n;)
27     {
28         int st = i, len = 1;
29         while(s[++i] == s[st])len++;
30         //cout<<s[st]<<" "<<st<<" "<<len<<endl;
31         q.push(node(s[st], st, len));
32     }
33     for(int i = 0; i < n; i++)
34     {
35         pre[i] = i - 1, next[i] = i + 1;
36     }
37     memset(vis, 0, sizeof(vis));
38     while(!q.empty())
39     {
40         node now = q.top();
41         q.pop();
42         if(now.len <= 1)break;
43         if(vis[now.pos])continue;
44         printf("%c", now.c);
45         int head = pre[now.pos], tail = now.pos;
46         for(int i = 0; i < now.len; i++, tail = next[tail])
47         {
48             vis[tail] = 1;
49             printf(" %d", tail + 1);
50         }
51         puts("");
52         if(head >= 0)next[head] = tail;
53         if(tail < n)pre[tail] = head;
54         if(head < 0 || tail >= n || s[head] != s[tail])continue;
55
56         int len = 2;
57         while(pre[head] >= 0 && s[pre[head]] == s[head])
58             head = pre[head], len++;
59         while(next[tail] < n && s[next[tail]] == s[tail])
60             tail = next[tail], len++;
61         q.push(node(s[head], head, len));
62     }
63     return 0;
64 }

转载于:https://www.cnblogs.com/fzl194/p/9336901.html

POJ - 3476 A Game with Colored Balls---优先队列+链表(用数组模拟)相关推荐

  1. poj 1724 有限制的最短距离(优先队列+链表)

    题目链接:http://poj.org/problem?id=1724 题目大意:给你一个最大费用,让你在费用容许的范围内(情况下)求有源点到终点的最短距离,也就是一般的单源最小距离,不同之处在于加了 ...

  2. 51Nod 1453(CF553-A) - 抽彩球(Kyoya and Colored Balls) - 解题报告

    51Nod 1453 - 抽彩球 - 解题报告 51Nod 1453 - 抽彩球 - 解题报告 Information Source Description Input Output Sample S ...

  3. 【POJ - 1456】Supermarket (贪心,优先队列 或并查集)

    题干: A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod s ...

  4. codeforces Kyoya and Colored Balls

    题解见:http://blog.csdn.net/libin56842/article/details/46650209 注意这里的组合数取模~~~ 1 /*Author :usedrose */ 2 ...

  5. C. Colored Balls: Revisited codeforces 1728A

    Problem - 1728A - Codeforces 题目大意:有n个袋子,每个袋子有cnt个球,每次可以拿出两个不同袋子的球,问最后可能剩下的是编号为多少的袋子 思路:奇数个球每次拿2个,最后至 ...

  6. POJ 1195 Mobile phones(裸的二维树状数组)

    http://poj.org/problem?id=1195 题意:给出一个矩阵,给某个格子加/减一个数,就某个子矩阵的和,1024*1024的范围,二维的树状数组 子矩阵(x1,y1,x2,y2)( ...

  7. *【CodeForces - 574A】Bear and Elections (优先队列,水题模拟)

    题干: Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections a ...

  8. poj 3486 A Simple Problem with Integers(树状数组第三种模板改段求段)

    1 /* 2 树状数组第三种模板(改段求段)不解释! 不明白的点这里:here! 3 */ 4 #include<iostream> 5 #include<cstring> 6 ...

  9. poj 3083 Children of the Candy Corn(bfs+dfs 数组模拟方向)

    好纠结啊,方向转晕了~~~~~先贴个半山寨的代码 #include <cstdio>#include<string.h>#define MAX 45 struct node{i ...

最新文章

  1. 3 calender python_python3笔记二十一:时间操作datetime和calendar
  2. linux mysql无符号整型_Mysql基础
  3. linux方向键ascii_上下左右 方向键的ASCII码值是多少?
  4. Java多线程:线程间通信之volatile与sychronized
  5. java this关键字的使用_Java this 关键字的使用方法详解
  6. Linux下mail/mailx命令发送邮件
  7. Xftp连接Linux 虚拟机
  8. pointofix 全局快捷键_屏幕画笔(Pointofix)
  9. 网络浏览器大战(Google与IE的较量)
  10. 在ubuntu系统中安装sublime
  11. 风变编程:是课程也是游戏,学习也能很简单
  12. 50岁的程序员该何去何从
  13. JPA以外键为条件查询出的List(外键过滤并存入JSONObject)
  14. 神经网络的图像识别技术,神经网络图像角度分析
  15. java 过滤http请求头_JAVAWEB开发实现对请求头、请求参数的过滤
  16. [flex]flex-direction: column;
  17. PSM倾向得分匹配代码和案例数据
  18. PCIe协议总结1-TLP(1)
  19. IntelliJ IDEA中文插件
  20. 网路3d虚拟三维展馆开发提高展馆的受众范围

热门文章

  1. 小米十年,雷军的一往无前
  2. 可怕!如果张东升是个程序员......
  3. Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
  4. 60、IPv6配置实验之RIP
  5. Java基础学习总结(11)——重载与重写
  6. codeforces 282E Sausage Maximization
  7. Hexo+腾讯云COS,为你的站点加速
  8. bzoj千题计划201:bzoj1820: [JSOI2010]Express Service 快递服务
  9. Repository 设计模式介绍
  10. do_exit——exit_notify()【转】