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
 
  解题:贪心+离散
  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
#define pii pair<int,int>
using namespace std;
const int maxn = ;
int p[maxn];
int d[maxn],n;
int ans[maxn],cnt,tot;
vector<int>g[maxn];
int main(){
while(~scanf("%d",&n)){
for(int i = ; i < n; ++i){
scanf("%d",d+i);
p[i]= d[i];
}
for(int i = tot = ; i < maxn; ++i) g[i].clear();
sort(d,d+n);
for(int i = cnt = ; i < n; ++i){
if(d[i] == d[cnt-]) continue;
d[cnt++] = d[i];
}
bool flag = false;
for(int i = ; i < n; ++i){
int index = lower_bound(d,d+cnt,p[i]) - d;
g[index].push_back(i+);
if(g[index].size() == ){
for(int j = ,k = g[index].size(); j < k; ++j)
ans[tot++] = g[index][j];
for(int i = ; i < cnt; ++i) g[i].clear();
flag = true;
}
if(g[index].size() == ){
int k;
for(k = ; k < cnt; ++k){
if(k == index) continue;
if(g[k].size() >= ) break;
}
if(k < cnt){
flag = true;
for(int j = ; j < ; ++j)
ans[tot++] = g[k][j];
for(int j = ; j < ; ++j)
ans[tot++] = g[index][j];
for(k = ; k < n; ++k) g[k].clear();
}
}
}
sort(ans,ans+tot);
if(flag){
printf("%d\n",tot>>);
for(int i = ; i < tot; i += )
printf("%d %d %d %d\n",ans[i],ans[i+],ans[i+],ans[i+]);
}else puts("");
putchar('\n');
}
return ;
}

最新文章

  1. ADO.NET 访问数据库
  2. MyBatis学习(二)、SQL语句映射文件(1)resultMap
  3. 使用sqoop将mysql数据导入到hadoop
  4. (转载)直接用SQL语句把DBF导入SQLServer
  5. BNUOJ-26482 Juice 树形DP
  6. 代码中实际运用memcached——.NET
  7. leetcode第一刷_Minimum Path Sum
  8. UIKit控件直接显示网页文字内容
  9. MapReduce深度分析(一)
  10. AspNet Core Api Restful +Swagger 实现微服务之旅 (三)
  11. java类的继承的一些细节
  12. python+selenium+autoit实现文件上传
  13. ES6知识整理(6)--Symbol函数
  14. django框架--路由系统
  15. UIViewController的初始化
  16. python---Django中模型类中Meta元对象了解
  17. RHEL-7.0重置root密码
  18. 51nod1459迷宫问题—(迪杰斯特拉)
  19. Linux上调试core文件(Good)
  20. 如​何​使​用​P​H​P​开​发​高​效​的​W​E​B​系​统

热门文章

  1. 原生JS封装ajax以及request
  2. IOS系统兼容input keyup事件
  3. POJ 2888
  4. I hate it (线段树)
  5. Codeforces 10A-Power Consumption Calculation(模拟)
  6. leetCode(46):Kth Smallest Element in a BST
  7. java中的system.out.println()和JSP中out.println()差别
  8. 使用Xcode-debug模式和release模式
  9. php利用msqli访问数据库并实现分页,
  10. ElasticSearch 深入理解 三:集群部署设计