ural 2071. Juice Cocktails
2016-03-04 11:14
549 查看
2071. Juice Cocktails
Time limit: 1.0 secondMemory limit: 64 MB
Once n Denchiks come to the bar and each orders a juice cocktail. It could be from 1 to 3 different juices in each cocktail. There are three juices in the bar: apple, banana and pineapple juice.
Every Denchik has his favourite juice cocktail. The cocktail is uniquely defined by those kinds of juices which it contains. Of course, each Denchik ordered his favorite cocktail. The barman can put all n glasses in a long row and pour the juice in the segment of successive glasses.
The bar is very popular, so the barman wants to serve Denchiks as quickly as possible. So, he wants to arrange glasses in a certain way to minimize the amount of "pourings." For one "pouring" he can pour one kind of juice in one segment of successive glasses.
As it is known, apple juice has higher density than banana juice. And density of banana juice is higher than that of pineapple one. Therefore, not to mix juices in a cocktail, you should firstly pour the apple, then banana and finally pineapple juice.
Of course, you must pour only that juices into each glass which are parts of a certain cocktail, but the relative order of juices should remain unchanged. There‘re too many Denchiks, so the barman can‘t understand how should he arrange glassess. Could you?
Input
The first line contains integer n (1 ≤ n ≤ 105).Next n lines describe Denchiks‘orders. Each order is described by one line, which consists of from 1 to 3 title Latin letters — A, B, P. The i-th line gives the kinds of juice included in the i-th order (A = Apple, B = Banana, P = Pineapple). The letters inside the string are arranged in this only order — "ABP", so to make the string you should eliminate some letters (perhaps, none) from "ABP" combination.
Output
In the first line print the minimal number of "pourings."In the second line output the indices of glasses in order in which they have to be arranged in row. Assume that the orders and corresponding glasses are numbered from 1 to n in the input order.
If there are several optimal ways of glasses arrangement, output any of them.
Sample
input | output |
---|---|
3 A B ABP | 3 1 3 2 |
Problem Source: Ural Regional School Programming Contest 2015
Tags: none (hide tags for unsolved problems)
Difficulty: 428
题意:给出n个串,只由A、B、P三个字母组成,且顺序是ABP,即它们的相对顺序就是ABP
每次可以消去连续一些串的相同的首字母,比如
ab -> b
ap p
问在最开始自有安排它们顺序后,最少多少次可以消去全部,输出方案
分析:
首先相同构成的肯定放在一起
一共只有7种串,暴力枚举他们的排列,然后暴力贪心的消去
#include <iostream> #include <algorithm> #include <vector> #include <cstring> using namespace std; #define INF (1000000001) #define sz(x) ((int) (x).size()) #define pub push_back const int N = 100010; const string STORE[] = {"A", "AB", "B", "BP", "ABP", "AP", "P"}; int n, m; string data ; vector<int> cnt[7]; int arr , it , pos[7]; string origin[7], tmp[7]; int ansPos[7]; inline void input() { cin >> n; for(int i = 0; i < n; i++) cin >> data[i]; } inline int work() { char ch[3] = {'A', 'B', 'P'}; int it[7] = {0}, ret = 0; for(int t = 0; t < 3; t++) for(int i = 0; i < m; i++) if(tmp[i][it[i]] == ch[t]) { it[i]++; int j = i; while(j + 1 < m) { if(it[j + 1] >= sz(tmp[j + 1])) break; if(tmp[j + 1][it[j + 1]] != ch[t]) break; j++; it[j]++; } ret++; i = j; } return ret; } inline int num(string &str) { int x; if(str == "A") x = 0; else if(str == "AB") x = 1; else if(str == "B") x = 2; else if(str == "BP") x = 3; else if(str == "ABP") x = 4; else if(str == "AP") x = 5; else if(str == "P") x = 6; return x; } inline bool cmp(string a, string b) { int x = num(a), y = num(b); return x < y; } inline void solve() { for(int i = 0; i < n; i++) if(data[i] == "A") cnt[0].pub(i); else if(data[i] == "AB") cnt[1].pub(i); else if(data[i] == "B") cnt[2].pub(i); else if(data[i] == "BP") cnt[3].pub(i); else if(data[i] == "ABP") cnt[4].pub(i); else if(data[i] == "AP") cnt[5].pub(i); else if(data[i] == "P") cnt[6].pub(i); m = 0; for(int i = 0; i < 7; i++) if(sz(cnt[i])) { pos[m] = i; m++; } int ans = INF; do { for(int i = 0; i < m; i++) tmp[i] = STORE[pos[i]]; int o = work(); if(ans > o) { ans = o; for(int i = 0; i < m; i++) ansPos[i] = pos[i]; } } while(next_permutation(pos, pos + m)); int out = 0; printf("%d\n", ans); for(int i = 0; i < m; i++) for(int j = 0; j < sz(cnt[ansPos[i]]); j++) { out++; if(out == n) printf("%d\n", cnt[ansPos[i]][j] + 1); else printf("%d ", cnt[ansPos[i]][j] + 1); } } int main() { ios::sync_with_stdio(0); input(); solve(); return 0; }
View Code
相关文章推荐
- Kendo UI
- 【实战Java高并发程序设计 7】让线程之间互相帮助--SynchronousQueue的实现
- iOS9新特性——堆叠视图UIStackView(2)
- IOS 多个UIImageView 加载高清大图时内存管理
- EasyUI Layout 布局
- UI动画架构(上下滚动,里面View可以实现动画<透明,移动,缩放,变色>l)
- iOS9新特性——堆叠视图UIStackView
- easyui datagrid 获取 title
- 1099. Build A Binary Search Tree (30)
- [LeetCode]Increasing Triplet Subsequence
- 让你明白response.sendRedirect()与request.getRequestDispatcher().forward()区别
- Easyui CSS式样重写
- IOS UIAlertController 弹框 (ios 9.0 后代替了UIAlertView弹框 和 UIActionSheet下弹框)
- android requestWindowFeature使用详解
- 从源码分析 对 非UI线程不允许访问UI元素的 理解
- UIAutomator remote debug方法
- Uiautomator使用
- EasyUI 的各种提示框消息框配置
- Android Studio 中遇到的Gradle build插件版本太低的问题:Plugin is too old
- SVN UUID 变更导致提交更新错误