贪心。能凑成一组就算一组

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

ZOJ 2702 Unrhymable Rhymes 贪心的更多相关文章

  1. ZOJ 2702 Unrhymable Rhymes

    Unrhymable Rhymes Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu De ...

  2. ZOJ 2702 Unrhymable Rhymes(DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1702 题目大意:给定有很多数字组成的诗,譬如 “AABB”, “AB ...

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

    题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

  4. 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)

    Numbers Time Limit: 2 Seconds      Memory Limit: 65536 KB DreamGrid has a nonnegative integer n . He ...

  5. ZOJ 3946 Highway Project 贪心+最短路

    题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...

  6. zoj 2921 Stock(贪心)

    Optiver sponsored problem. After years of hard work Optiver has developed a mathematical model that ...

  7. zoj 1375||poj 1230(贪心)

    Pass-Muraille Time Limit: 2 Seconds      Memory Limit: 65536 KB In modern day magic shows, passing t ...

  8. POJ - 2349 ZOJ - 1914 Arctic Network 贪心+Kru

    Arctic Network The Department of National Defence (DND) wishes to connect several northern outposts ...

  9. ZOJ 3870 Team Formation 贪心二进制

                                                    B - Team Formation Description For an upcoming progr ...

随机推荐

  1. mybatis多对多关联查询——(十)

    1.需求 查询用户及用户购买商品信息. 2     sql语句 查询主表是:用户表 关联表:由于用户和商品没有直接关联,通过订单和订单明细进行关联,所以关联表: orders.orderdetail. ...

  2. python3光学字符识别模块tesserocr与pytesseract

    OCR,即Optical Character Recognition,光学字符识别,是指通过扫描字符,然后通过其形状将其翻译成电子文本的过程,对应图形验证码来说,它们都是一些不规则的字符,这些字符是由 ...

  3. Linux Timer定时器【转】

    转自:https://www.jianshu.com/p/66b3c75cae81 timerfd为Linux为用户程序提供的定时器接口,该接口基于文件描述符,通过文件描述符的可读事件进行超时通知,且 ...

  4. 内核早期内存分配器:memblock

    内核早期内存分配器:memblockLinux内核使用伙伴系统管理内存,那么在伙伴系统工作前,如何管理内存?答案是memblock.memblock在系统启动阶段进行简单的内存管理,记录物理内存的使用 ...

  5. Angularjs里面跨作用域的实战!

    好久没有来写博客了,最近一直在用Google的AngularJS,后面我自己简称AngularJS就叫AJ吧! 学习AngularJS一路也是深坑颇多啊--!就不多说了,不过还是建议大家有时间去学下下 ...

  6. OS X 10.11:在exFAT分区的外置硬盘上使用Time Machine。

    Time Machine默认需要使用HFS+分区的外置硬盘,但在网络硬盘上也可以使用单个的 .sparsebundle 镜像文件备份.在本地USB或Firewire等接口连接的外置硬盘,只有exFAT ...

  7. LINUX下IDEA等工具调试项目时提示:Unable to open debugger port

    在Ubuntu下调试项目时使用TOMCAT容器,在设置好相应的TOMCAT LOCAL 路径及相关信息后,点击调试项目出现: Unable to open debugger port : java.n ...

  8. PHPWeb开发相关知识

    转载:https://blog.csdn.net/wj610671226/article/details/78426698 正则表达式 作用:分割.查找.匹配.替换字符串 分割符:正斜线(/).has ...

  9. javascript-序列化,时间,eval,转义

    一:序列化 JSON.stringify(li) 将对象转字符串 JSON.parse(str1) 将字符串转对象 li=[11,22,33] [11, 22, 33] li [11, 22, 33] ...

  10. jdk8 lambda表达式总结

    Java8 lambda表达式10个示例   1. 实现Runnable线程案例 使用() -> {} 替代匿名类: //Before Java 8: new Thread(new Runnab ...