ZOJ 2702 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 ;
}
ZOJ 2702 Unrhymable Rhymes的更多相关文章
- ZOJ 2702 Unrhymable Rhymes 贪心
贪心.能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds Memory Limit: 32768 KB Special Judge ...
- ZOJ 2702 Unrhymable Rhymes(DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1702 题目大意:给定有很多数字组成的诗,譬如 “AABB”, “AB ...
- ZOJ题目分类
ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
- ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...
随机推荐
- [转载] Linux新手必看:浅谈如何学习linux
本文转自 https://www.cnblogs.com/evilqliang/p/6247496.html 本文在Creative Commons许可证下发布 一.起步 首先,应该为自己创造一个学习 ...
- python--(常用模块-1)
python--(常用模块-1) 一.模块的简单认识: 什么是模块,模块就是我们把装有特有功能的代码进行归类的结果,从代码编写的单位来看我们的程序,从小到大的顺序:一条代码<语句块<代码块 ...
- SpringBoot中打包设置,将配置文件打包在外部
一.每次用maven的打包工具打包的时候 总是将配置文件一起打包进jar中!配置文件有点小修改就要重新打包很麻烦!!!!为了解决这一麻烦!找 了很多方法,下面的配置已经实现可用 我的项目目录结构如下 ...
- 【codeforces 747E】Comments
[题目链接]:http://codeforces.com/problemset/problem/747/E [题意] 给你一个类似递归的结构; 让你把每一层的字符串按照层,一层层地输出出来; 并输出层 ...
- ZJU 2676 Network Wars
Network Wars Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original I ...
- POJ3684 Physics Experiment 【物理】
Physics Experiment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1031 Accepted: 365 ...
- 自己主动化測试程序之中的一个自己定义键盘的模拟測试程序(C语言)
一.測试程序编写说明 我们做的终端设备上运行的是QT应用程序.使用自己定义的键盘接口.经过測试人员长时间的人机交互測试,来确认系统的功能是否满足需求. 如今须要编写一个自己主动化的測试程序,能够依照预 ...
- Java推断类和实例的关系
通常我们使用instanceOf关键字来推断一个对象是否是类的实例,近期博主看到isInstance关键字,不解与instanceOf的差别,故度娘了一下,顺便涨了一下姿势. Java中推 ...
- iOS开发 之 不要告诉我你真的懂isEqual与hash!
目录 为什么要有isEqual方法? 如何重写自己的isEqual方法? 为什么要有hash方法? hash方法什么时候被调用? hash方法与判等的关系? 如何重写自己的hash方法? 为什么要有i ...
- 极客时间 mysql实战45讲下载读 08讲事务到底是隔离的还是不隔离的 笔记
笔记体会: 1.innodb支持RC和RR隔离级别实现是用的一致性视图(consistent read view) 2.事务在启动时会拍一个快照,这个快照是基于整个库的.基于整个库的意思就是说一个事务 ...