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 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


Source:  Andrew Stankevich's Contest #9



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn=4400;

int n;
vector<int> v[maxn];
vector<int> ans;

void init()
{
    for(int i=0; i<=n+10; i++) v[i].clear();
    ans.clear();
}

int a[maxn],b[maxn];

int main()
{
    bool first=true;
    while(scanf("%d",&n)!=EOF)
    {
        if(first) first=false;
        else putchar(10);

        init();
        for(int i=0; i<n; i++)
        {
            scanf("%d",a+i);
            b[i]=a[i];
        }
        sort(b,b+n);
        int m=unique(b,b+n)-b;
        int two=-1;
        for(int i=0;i<n;i++)
        {
            a[i]=lower_bound(b,b+m,a[i])-b;
            v[a[i]].push_back(i);
            int vas=v[a[i]].size();
            if(vas>=2)
            {
                if(two!=-1)
                {
                    if(two!=a[i])
                    {
                        ans.push_back(v[two][0]);
                        ans.push_back(v[two][1]);
                        ans.push_back(v[a[i]][0]);
                        ans.push_back(v[a[i]][1]);
                        /// clear
                        for(int j=0; j<=n+10; j++) v[j].clear();
                        two=-1;
                    }
                    else if(two==a[i]&&vas>=4)
                    {
                        for(int j=0;j<4;j++)
                        {
                            ans.push_back(v[a[i]][j]);
                        }
                        for(int j=0; j<=n+10; j++) v[j].clear();
                        two=-1;
                    }
                }
                else
                {
                    two=a[i];
                }
            }
        }
        printf("%d\n",(int)ans.size()/4);
        sort(ans.begin(),ans.end());
        for(int i=0,sz=ans.size();i<sz;i++)
        {
            printf("%d",ans[i]+1);
            if((i+1)%4==0) putchar(10);
            else putchar(32);
        }
    }
    return 0;
}








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5401366.html,如需转载请自行联系原作者  

相关文章
|
9月前
动态规划——OJ题(一)
动态规划——OJ题(一)
79 1
hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】(贪心)
hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】(贪心)
71 0
HDOJ 1081(ZOJ 1074) To The Max(动态规划)
HDOJ 1081(ZOJ 1074) To The Max(动态规划)
88 0
HDOJ 1081(ZOJ 1074) To The Max(动态规划)
|
并行计算 算法 Java
HDU 1874 畅通工程续【Floyd算法实现】
畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 53806    Accepted Submission(s): 20092 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路。
1095 0
|
Java 测试技术 C++
HDU 3783 ZOJ
ZOJ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2779    Accepted Submission(s): 1840 Problem Description 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出。
1119 0
【OJ】DP 01背包 记忆化搜索 O(nW)
题目链接:点击打开链接 /* 01背包 记忆化搜索 O(nW) */ #include #include #include #define MAX_N 101 #define MAX_W 3001 using namespace std;//最多有3000元,dp[...
928 0
|
机器学习/深度学习
【OJ】贪心法 Saruman's Army POJ 3069 /acmclub 12132
题目链接:点击打开链接 /* 6 10 贪心法Saruman's Army POJ 3069 1 7 15 20 30 50 ans=3 */ #include #include using namespace std; int x[1010]; ...
892 0
|
机器学习/深度学习
【OJ】贪心法 Saruman&#39;s Army POJ 3069 /acmclub 12132
题目链接:点击打开链接 /* 6 10 贪心法Saruman's Army POJ 3069 1 7 15 20 30 50 ans=3 */ #include #include using namespace std; int x[1010]; int main(){ // freopen("贪心法 Saruman's Army poj3069.
850 0