Zoj 2702 unrhymable rhymes

Source: Internet
Author: User
Tags integer numbers
Unrhymable rhymes

Time limit:10000 msMemory limit:32768kb64bit 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 preparedNLines of the future poem, but suddenly noticed that not all of them rhyme well.

Though extends actionist, Willy stronugly respects canons of classic poetry. he is going to write the poem that wowould 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 containsN--- The number of lines Willy has composed (1 <= n <= 4000). It is followedNInteger numbers denoting the rhyme groups that lines of the poem belong to. All numbers are positive and do not exceed109.

There is an empty line after each case.

 

Output

 

On the first line of the output file printK--- The maximal number of quatrains Willy can make. After that print4 KNumbers --- the lines that shoshould form the poem.

There shoshould be an empty line after each case.

 

Sample Input

 

151 2 3 1 2 1 2 3 3 2 1 1 3 2 231 2 3

 

Sample output

 

31 2 4 57 8 9 1011 12 14 150
Problem solving: greedy + discrete
 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 long14 #define INF 0x3f3f3f3f15 #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 }
View code

 

Zoj 2702 unrhymable rhymes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.