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 = ...
随机推荐
- 如何让select中的滚动条自动定位到框中选中项的位置
document.getElementById("hidScrollTop").value = document.getElementById("slcYZYongFa& ...
- tensorflow之tf.slice()
转载:https://www.jianshu.com/p/71e6ef6c121b https://www.cnblogs.com/chamie/p/11073363.html def slice(i ...
- [剑指offer] 8+9. 跳台阶+变态跳台阶 (递归 时间复杂度)
跳台阶是斐波那契数列的一个典型应用,其思路如下: # -*- coding:utf-8 -*- class Solution: def __init__(self): self.value=[0]*5 ...
- 洛谷P1004 方格取数
网络流大法吼 不想用DP的我选择了用网络流-- 建模方法: 从源点向(1,1)连一条容量为2(走两次),费用为0的边 从(n,n)向汇点连一条容量为2,费用为0的边 每个方格向右边和下边的方格连一条容 ...
- Java并发和多线程2:3种方式实现数组求和
本篇演示3个数组求和的例子. 例子1:单线程例子2:多线程,同步求和(如果没有计算完成,会阻塞)例子3:多线程,异步求和(先累加已经完成的计算结果) 例子1-代码 package cn.fansuni ...
- Vue 做项目经验
Vue 做项目经验 首先需要知道最基本的东西是: Vue 项目打包:npm run build Vue生成在网页上看的端口:npm run dev 修改端口号的地方在: config文件夹下index ...
- 简述vuex的数据传递流程
简述vuex的数据传递流程 当组件进行数据修改的时候我们需要调用dispatch来触发actions里面的方法.actions里面的每个方法中都会有一个commit方法,当方法执行的时候会通过comm ...
- 玩转iOS开发 - Runloop 具体解释
Runloop 具体解释
- Spring+mybatis+struts框架整合的配置具体解释
学了非常久的spring+mybatis+struts.一直都是单个的用他们,或者是两两组合用过,今天总算整合到一起了,配置起来有点麻烦.可是配置完一次之后.就轻松多了,那么框架整合配置具体解释例如以 ...
- HDU 4175 Class Schedule (暴力+一点dp)
pid=4175">HDU 4175 题意:有C座楼,每座楼有T个教室.一个人须要訪问C个教室.每座楼仅仅能訪问一个教室. 訪问教室须要消耗能量,从x点走到y点须要消耗abs(x-y) ...