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的更多相关文章

  1. ZOJ 2702 Unrhymable Rhymes 贪心

    贪心.能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special Judge ...

  2. ZOJ 2702 Unrhymable Rhymes(DP)

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

  3. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  4. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  5. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  6. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  7. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  8. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  9. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

随机推荐

  1. php5 中魔术方法函数有哪几个

    魔术函数:9.3 构造函数:__construct() 9.3.1 实例化对象时被调用. 9.3.2 在类中,构造函数是用来初始化对象的,利用构造函数,可以操作对象,并改变它的值. 9.3.3 当__ ...

  2. nutch如何修改regex-urlfilter.txt爬取符合条件的链接

    例如我在爬取学生在线的时候,发现爬取不到特定的通知,例如<中粮福临门助学基金申请公告>,通过分析发现原来通知的链接被过滤掉了,下面对过滤url的配置文件regex-urlfilter.tx ...

  3. 洛谷—— P3388 【模板】割点(割顶)

    https://www.luogu.org/problem/show?pid=3388 题目背景 割点 题目描述 给出一个n个点,m条边的无向图,求图的割点. 输入输出格式 输入格式: 第一行输入n, ...

  4. cogs 2752. [济南集训 2017] 数列运算

    2752. [济南集训 2017] 数列运算 ★★☆   输入文件:sequenceQBXT.in   输出文件:sequenceQBXT.out   简单对比时间限制:1 s   内存限制:512 ...

  5. python多线程编程代码

    参考了这篇文章,写的挺好的. http://blog.csdn.net/abcjennifer/article/details/49474897 import time import threadin ...

  6. ubuntu系统AndroidStudio改动内存大小

    位于android-studio/bin文件夹下的studio64.vmoptions和studio.vmoptions文件. 把Xms,Xmx,-XX:MaxPermSize.-XX:Reserve ...

  7. c# 获取文件夹下面所有文件夹列表

    方法一: string dirPath = @"D:\App1"; List<string> dirs = new List<string>(Directo ...

  8. bzoj5105: [CodePlus2017]晨跑(LCM)

    5105: [CodePlus2017]晨跑 题目:传送门 题解: 没有很懂Code Puls 的操作...一道签到的三个数的LCM??? 代码: #include<cstdio> #in ...

  9. mysql表空间传输(ERROR 1808) row_format设置

    文章结构如下: 从MYSQL5.6版本开始,引入了传输表空间这个功能,可以把一张表从一个数据库移到另一个数据库或者机器上.迁移的时候很方便,尤其是大表. 由于本次达到测试使用版本5.6.38传到5.7 ...

  10. IDEA模板设置

    /**   * @className: $CLASSNAME$   * @author: liuyachao   * @date: $DATE$ $TIME$ */ ================= ...