The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
A sequence S = {s1, s2, ..., sn} is called heapable if there exists a binary tree T with n nodes such that every node is labelled with exactly one element from the sequence S, and for every non-root node si and its parent sj, sj ≤ si and j < i hold. Each element in sequence S can be used to label a node in tree T only once.
Chiaki has a sequence a1, a2, ..., an, she would like to decompose it into a minimum number of heapable subsequences.
Note that a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contain an integer n (1 ≤ n ≤ 105) — the length of the sequence.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).
It is guaranteed that the sum of all n does not exceed 2 × 106.
Output
For each test case, output an integer m denoting the minimum number of heapable subsequences in the first line. For the next m lines, first output an integer Ci, indicating the length of the subsequence. Then output Ci integers Pi1, Pi2, ..., PiCi in increasing order on the same line, where Pij means the index of the j-th element of the i-th subsequence in the original sequence.
Sample Input
4
4
1 2 3 4
4
2 4 3 1
4
1 1 1 1
5
3 2 1 4 1
Sample Output
1
4 1 2 3 4
2
3 1 2 3
1 4
1
4 1 2 3 4
3
2 1 4
1 2
2 3 5 题意:序列 S={s1,s2,...,sn} 被称为 heapable ,当且仅当存在一个二叉树 T , n 个点均作为 T 的一个节点,且满足任意非根节点 si 和其父节点 sj , sj≤si 且 j<i 。
现在有一个序列 a1,a2,...,an ,相应将其分成若干个 heapable 的子序列,问使得子序列个数最少的策略
题解:参考自别人的代码 贪心+二分 用memset会TLE 直接for一遍清零。
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#pragma comment(linker, "/STACK:102400000,102400000")
int a[];
struct node
{
int f,s;
}p;
bool operator <(node x,node y)
{
if(a[x.s]==a[y.s]) return x.s<y.s;
return a[x.s]<a[y.s];
}
set<node> st;
set<node> :: iterator it;
vector<int> v[];
int dis[];
int main()
{
int t;
scanf("%d",&t);
for(int i=;i<=t;i++)
{
st.clear();
int cnt=;
int n;
scanf("%d",&n);
for(int k=;k<=n;k++)
dis[k]=;
for(int j=;j<=n;j++)
{
scanf("%d",&a[j]);
p.s=j;
it=st.upper_bound(p);
if(it==st.begin())
{
v[cnt].push_back(j);
p.f=cnt;
p.s=j;
st.insert(p);
cnt++;
}
else
{
it--;
p=*it;
dis[p.s]++;
if(dis[p.s]==)
st.erase(it);
p.s=j;
st.insert(p);
v[p.f].push_back(j);
}
}
printf("%d\n",cnt);
for(int j=;j<cnt;j++)
{
printf("%d",v[j].size());
for(int k=;k<v[j].size();k++)
printf(" %d",v[j][k]);
printf("\n");
v[j].clear();
}
}
return ;
}
The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分的更多相关文章
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - C 暴力 STL
What Kind of Friends Are You? Time Limit: 1 Second Memory Limit: 65536 KB Japari Park is a larg ...
- ZOJ 3962 E.Seven Segment Display / The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple E.数位dp
Seven Segment Display Time Limit: 1 Second Memory Limit: 65536 KB A seven segment display, or s ...
- The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - L Doki Doki Literature Club
Doki Doki Literature Club Time Limit: 1 Second Memory Limit: 65536 KB Doki Doki Literature Club ...
- 2018浙江省赛(ACM) The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple
我是铁牌选手 这次比赛非常得爆炸,可以说体验极差,是这辈子自己最脑残的事情之一. 天时,地利,人和一样没有,而且自己早早地就想好了甩锅的套路. 按理说不开K就不会这么惨了啊,而且自己也是毒,不知道段错 ...
- The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - M Lucky 7
Lucky 7 Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found a positive integer se ...
- The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - J CONTINUE...?
CONTINUE...? Time Limit: 1 Second Memory Limit: 65536 KB Special Judge DreamGrid has clas ...
- The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - B King of Karaoke
King of Karaoke Time Limit: 1 Second Memory Limit: 65536 KB It's Karaoke time! DreamGrid is per ...
- The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple -A Peak
Peak Time Limit: 1 Second Memory Limit: 65536 KB A sequence of integers is called a peak, if ...
- ZOJ 4033 CONTINUE...?(The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple)
#include <iostream> #include <algorithm> using namespace std; ; int a[maxn]; int main(){ ...
随机推荐
- No.03---Vue学习之路之模块化组织
前两篇讲解了一下 Vuex 的基本使用方法,可是在实际项目中那么写肯定是不合理的,如果组件太多,不可能把所有组件的数据都放到一个 store.js 中的,所以就需要模块化的组织 Vuex,首先看一下 ...
- 简单在kubernetes中安装cadvisor
cadvisor用于分析docker资源占用情况及性能的工具 安装命令: docker run --volume=/:/rootfs:ro --volume=/: --detach=true --na ...
- 特征点检测--基于CNN:TILDE: A Temporally Invariant Learned DEtector
TILDE: A Temporally Invariant Learned DEtector Yannick Verdie1,∗ Kwang Moo Yi1,∗ Pascal Fua1 Vincent ...
- 数据库mysql的常规操作
1. 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. 简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进 ...
- 基于kcp,consul的service mesh实现
名字kmesh 技术:proxy,kcp,consul proxy proxy分为前端和后端 前端代理服务层,包括外部的service 后端实现负债均衡 kcp kcp 基于udp,能够实现快速的传输 ...
- [T-ARA/筷子兄弟][Little Apple]
歌词来源:http://music.163.com/#/song?id=29753511 作曲 : 筷子兄弟 [作曲 : 筷子兄弟] 作词 : K-Smith [作词 : KSmith] 编曲 : 新 ...
- redis 学习记录
http://www.yiibai.com/redis/redis_quick_guide.html Redis 是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and s ...
- Sqlserver学习研究
关注关键词 :Sqlserver实用工具配置步骤 1)创建实用工具控制点(UCP) 2)连接到现有UCP 3)相UCP注册SQL Server实例 4)创建数据层应用程序 5)设置资源运行状况策略 6 ...
- C#控制台应用程序
使用C#创建控制台应用程序的基本步骤: (1)创建项目: (2)编辑C#源代码: (3)编译运行: 例题:在控制台输出“Hello world!”. 第一步:文件→新建→项目:选择“项目类型”为Vis ...
- 3dContactPointAnnotationTool开发日志(二九)
今天想着在Windows平台上跑通那个代码,不过它的官网上写的支持平台不包括windows,但我还是想试试,因为看他的依赖好像和平台的关系不是特别大. 看了下它的py代码,不知道是py2还是p ...