ZOJ 2702 Unrhymable Rhymes

Unrhymable Rhymes

Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

An amateur poet Willy is going to write his first abstract poem. Since abstract art does not give much care to the meaning of the poem, Willy is planning to impress listeners with unusual combinations of words. He prepared n lines of the future poem, but suddenly noticed that not all of them rhyme well.

Though abstractionist, Willy strongly respects canons of classic poetry. He is going to write the poem that would consist of quatrains. Each quatrain consists of two pairs of rhymed lines. Therefore there can be four types of quatrains, if we denote rhymed lines with the same letter, these types are ?AABB?, ?ABAB?, ?ABBA? and ?AAAA?.

Willy divided the lines he composed into groups, such that in each group any line rhymes with any other one. He assigned a unique integer number to each group and wrote the number of the group it belongs next to each line. Now he wants to drop some lines from the poem, so that it consisted of correctly rhymed quatrains. Of course, he does not want to change the order of the lines.

Help Willy to create the longest poem from his material.

Input

There are mutilple cases in the input file.

The first line of each case contains n --- the number of lines Willy has composed (1 <= n <= 4000 ). It is followed by n integer numbers denoting the rhyme groups that lines of the poem belong to. All numbers are positive and do not exceed 109 .

There is an empty line after each case.

Output

On the first line of the output file print k --- the maximal number of quatrains Willy can make. After that print 4k numbers --- the lines that should form the poem.

There should be an empty line after each case.

Sample Input

15
1 2 3 1 2 1 2 3 3 2 1 1 3 2 2

3
1 2 3

Sample Output

3
1 2 4 5
7 8 9 10
11 12 14 15

0

  解题:贪心+离散

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 #define pii pair<int,int>
16 using namespace std;
17 const int maxn = 5100;
18 int p[maxn];
19 int d[maxn],n;
20 int ans[maxn],cnt,tot;
21 vector<int>g[maxn];
22 int main(){
23     while(~scanf("%d",&n)){
24         for(int i = 0; i < n; ++i){
25             scanf("%d",d+i);
26             p[i]= d[i];
27         }
28         for(int i = tot = 0; i < maxn; ++i) g[i].clear();
29         sort(d,d+n);
30         for(int i = cnt = 1; i < n; ++i){
31             if(d[i] == d[cnt-1]) continue;
32             d[cnt++] = d[i];
33         }
34         bool flag = false;
35         for(int i = 0; i < n; ++i){
36             int index = lower_bound(d,d+cnt,p[i]) - d;
37             g[index].push_back(i+1);
38             if(g[index].size() == 4){
39                 for(int j = 0,k = g[index].size(); j < k; ++j)
40                     ans[tot++] = g[index][j];
41                 for(int i = 0; i < cnt; ++i) g[i].clear();
42                 flag = true;
43             }
44             if(g[index].size() == 2){
45                 int k;
46                 for(k = 0; k < cnt; ++k){
47                     if(k == index) continue;
48                     if(g[k].size() >= 2) break;
49                 }
50                 if(k < cnt){
51                     flag = true;
52                     for(int j = 0; j < 2; ++j)
53                         ans[tot++] = g[k][j];
54                     for(int j = 0; j < 2; ++j)
55                         ans[tot++] = g[index][j];
56                     for(k = 0; k < n; ++k) g[k].clear();
57                 }
58             }
59         }
60         sort(ans,ans+tot);
61         if(flag){
62             printf("%d\n",tot>>2);
63             for(int i = 0; i < tot; i += 4)
64                 printf("%d %d %d %d\n",ans[i],ans[i+1],ans[i+2],ans[i+3]);
65         }else puts("0");
66         putchar(‘\n‘);
67     }
68     return 0;
69 }

时间: 2024-08-09 19:51:55

ZOJ 2702 Unrhymable Rhymes的相关文章

ZOJ 2702 Unrhymable Rhymes 贪心

贪心,能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special Judge An amateur poet Willy is going to write his first abstract poem. Since abstract art does not give much care to the meaning of the poem, Willy is plan

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

ZOJ 2859 二维线段树

思路:自己写的第二发二维线段树1A,哈哈,看来对二维的push操作比较了解了:但是还没遇到在两个线段树中同时进行push操作的,其实这题我是想在x维和y维同时进行push操作的,但是想了好久不会,然后看到这题又给出10秒,然后想想在x维线段直接单点查询肯定也过了,然后在第二维就只有pushup操作,在第一维线段树没有pushup操作.要是在第一维也有pushup操作的话,那就不用单点查询那么慢了.不过也A了,想找题即在二维同时进行pushup和pushdown操作的. #include<iost

ZOJ 2588

求一个无向图的桥(可能存在重边),输出割边的数目,并按顺序输出割边的序号(输入的顺序). 由于内存的限制 , 无法使用邻接矩阵 , 只能用邻接表了 . 第一次用了邻接表,超内存了: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <string.h> 5 using namespace std; 6 const int N=10002; 7 const i

ZOJ 2587 Unique Attack 判断最小割是否唯一

很裸的判断最小割是否唯一.判断方法是先做一遍最大流求最小割,然后从源点和汇点分别遍历所有能够到达的点,看是否覆盖了所有的点,如果覆盖了所有的点,那就是唯一的,否则就是不唯一的. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostr