ural 2071. Juice Cocktails
2071. Juice Cocktails
Time limit: 1.0 second
Memory limit: 64 MB
Memory 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 Author: Mikhail Rubinchik (prepared by Nickolai Permyakov, Alexey Danilyuk)
Problem Source: Ural Regional School Programming Contest 2015
Problem Source: Ural Regional School Programming Contest 2015
Tags: none
Difficulty: 428
题意:给出n个串,只由A、B、P三个字母组成,且顺序是ABP,即它们的相对顺序就是ABP
每次可以消去连续一些串的相同的首字母,比如
ab -> b
ap p
问在最开始自有安排它们顺序后,最少多少次可以消去全部,输出方案
分析:
首先相同构成的肯定放在一起
一共只有7种串,暴力枚举他们的排列,然后暴力贪心的消去
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 #include <cstring> 5 using namespace std; 6 #define INF (1000000001) 7 #define sz(x) ((int) (x).size()) 8 #define pub push_back 9 10 const int N = 100010; 11 const string STORE[] = {"A", "AB", "B", "BP", "ABP", "AP", "P"}; 12 int n, m; 13 string data[N]; 14 vector<int> cnt[7]; 15 int arr[N], it[N], pos[7]; 16 string origin[7], tmp[7]; 17 int ansPos[7]; 18 19 inline void input() 20 { 21 cin >> n; 22 for(int i = 0; i < n; i++) cin >> data[i]; 23 } 24 25 inline int work() 26 { 27 char ch[3] = {'A', 'B', 'P'}; 28 int it[7] = {0}, ret = 0; 29 for(int t = 0; t < 3; t++) 30 for(int i = 0; i < m; i++) 31 if(tmp[i][it[i]] == ch[t]) 32 { 33 it[i]++; 34 int j = i; 35 while(j + 1 < m) 36 { 37 if(it[j + 1] >= sz(tmp[j + 1])) break; 38 if(tmp[j + 1][it[j + 1]] != ch[t]) break; 39 j++; 40 it[j]++; 41 } 42 ret++; 43 i = j; 44 } 45 return ret; 46 } 47 48 inline int num(string &str) 49 { 50 int x; 51 if(str == "A") x = 0; 52 else if(str == "AB") x = 1; 53 else if(str == "B") x = 2; 54 else if(str == "BP") x = 3; 55 else if(str == "ABP") x = 4; 56 else if(str == "AP") x = 5; 57 else if(str == "P") x = 6; 58 return x; 59 } 60 61 inline bool cmp(string a, string b) 62 { 63 int x = num(a), y = num(b); 64 return x < y; 65 } 66 67 inline void solve() 68 { 69 for(int i = 0; i < n; i++) 70 if(data[i] == "A") cnt[0].pub(i); 71 else if(data[i] == "AB") cnt[1].pub(i); 72 else if(data[i] == "B") cnt[2].pub(i); 73 else if(data[i] == "BP") cnt[3].pub(i); 74 else if(data[i] == "ABP") cnt[4].pub(i); 75 else if(data[i] == "AP") cnt[5].pub(i); 76 else if(data[i] == "P") cnt[6].pub(i); 77 78 m = 0; 79 for(int i = 0; i < 7; i++) 80 if(sz(cnt[i])) 81 { 82 pos[m] = i; 83 m++; 84 } 85 int ans = INF; 86 do 87 { 88 for(int i = 0; i < m; i++) tmp[i] = STORE[pos[i]]; 89 int o = work(); 90 if(ans > o) 91 { 92 ans = o; 93 for(int i = 0; i < m; i++) ansPos[i] = pos[i]; 94 } 95 } while(next_permutation(pos, pos + m)); 96 97 int out = 0; 98 printf("%d\n", ans); 99 for(int i = 0; i < m; i++) 100 for(int j = 0; j < sz(cnt[ansPos[i]]); j++) 101 { 102 out++; 103 if(out == n) printf("%d\n", cnt[ansPos[i]][j] + 1); 104 else printf("%d ", cnt[ansPos[i]][j] + 1); 105 } 106 } 107 108 int main() 109 { 110 ios::sync_with_stdio(0); 111 input(); 112 solve(); 113 return 0; 114 }