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 = ...
随机推荐
- java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
最近在使用java PiO导入导出Excle在windos本机上运行没有问题: 但是!!问题来了!放到Linux服务器上部署后出现异常 java.lang.NoClassDefFoundError: ...
- SparkSql初级编程实践
1.Spark SQL 基本操作将下列 JSON 格式数据复制到 Linux 系统中,并保存命名为 employee.json.{ "id":1 , "name" ...
- ResNet入门
CNN入门讲解:什么是残差网络Resnet 变种神经网络的典型代表:深度残差网络 深度残差神经网络ResNet 学习笔记 https://blog.csdn.net/loveliuzz/article ...
- java EE使用response返回中文时,出现乱码问题
response.setHeader("content-type", "text/html;charset=UTF-8");
- NOIP2018提高组金牌训练营——数论专题
地址 https://www.51nod.com/live/liveDescription.html#!liveId=23 1187 寻找分数 给出 a,b,c,d, 找一个分数p/q,使得a/b & ...
- 关于nodejs的线程模型可以看这篇文章
虽然还是有一些没有讲全,但是整体还是讲的很不错的. http://www.ruanyifeng.com/blog/2014/10/event-loop.html
- Cocos2d-x 3.0多线程异步资源载入
Cocos2d-x从2.x版本号到上周刚刚才公布的Cocos2d-x 3.0 Final版,其引擎驱动核心依然是一个单线程的"死循环".一旦某一帧遇到了"大活儿" ...
- Android Material Design-Getting Started(入门)-(一)
转载请注明出处:http://blog.csdn.net/bbld_/article/details/40400343 翻译自:http://developer.android.com/trainin ...
- Boost Log 基本使用方法
Boost Log 基本使用方法 flyfish 2014-11-5 依据boost提供的代码演示样例,学习Boost Log 的基本使用方法 前提 boost版本号boost_1_56_0 演示样例 ...
- 为powerpc交叉编译nginx
HOST: MINT NGINX VERSION: nginx-1.8.0(nginx-1.8.0.tar.gz) ZLIB VERSION: zlib-1.2.8 PCRE VERSION: pcr ...