Greedy, can be combined into a group, even if a group
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 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
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;}
Zoj 2702 unrhymable rhymes greedy