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;
}
时间: 2024-10-07 00:57:58

ZOJ 2702 Unrhymable Rhymes 贪心的相关文章

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

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 3778 Talented Chef 贪心

zoj 3778 Talented Chef 题意: 有n个饼,给出完成每个饼所需要的时间t1,t2,...,tn,现在有m个锅(也就是说可以同时煎m个饼),问完成所有饼至少需要多少时间. 限制: 1 <= n,m,ti <= 40000 思路: 贪心 ans=max(ceil(sigma(1~n,ti)/m),max(ti)) /*zoj 3778 Talented Chef 题意: 有n个饼,给出完成每个饼所需要的时间t1,t2,...,tn,现在有m个锅(也就是说可以同时煎m个饼),问完

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

ZOJ 3689 Digging(贪心+dp)

Digging Time Limit: 2 Seconds      Memory Limit: 65536 KB When it comes to the Maya Civilization, we can quickly remind of a term called the end of the world. It's not difficult to understand why we choose to believe the prophecy (or we just assume i

ZOJ 1025. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some

ZOJ 3829 Known Notation 贪心

主要的贪心思想就是,如果有一个不合法的*,那么再他前面加1或者2个数字的花费是不可能小于把它和后面的数字交换的,所以把不合法星号尽可能的往后放即可. 这里我因为懒得特判,把每个情况都算了,不过n只有1000,n^2也是可以接受的. #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <map&

POJ 1862 &amp; ZOJ 1543 Stripies(贪心 | 优先队列)

题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=543 Description Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian -

ZOJ 3699 Dakar Rally(贪心)

题意  路上有 n 个加油站  每个加油站的价格可能不同  你的油箱容积为 v  问从起点开车到终点加油至少用多少钱 贪心  每次都让油箱里面便宜的油最多就行了   在每个站点 i 有两种情况 1.  i 点把油加满跑完都没有更便宜的  那么在 i 点肯定要加满  然后开到 i+1 点去 2.  i 点把油加满能跑到第一个比 i 点更便宜的 j 点或者到了终点 j   那么只用把油加到能到 j 点  然后直接开到 j 点 然后把每个点加油用的钱加起来就行了 #include <cstdio>