Lena Sort 构造题,很多细节的模拟
https://www.hackerrank.com/contests/101hack46/challenges/lena-sort
把题目转换成一颗二叉树,也就是当前的节点是cur,然后大的,放右边,小的,放左边。
然后所有节点的深度总和,就是答案c。现在就是要你构造一颗这样的树。
最大的树就是左偏树,然后从最后一个节点开始模拟,如果它放移上一格,贡献就会 - 1,所以肯定能构造出一颗满意的树。
至于细节比较多,慢慢debug把。代码也非常混乱。
数据
15 18
7 12
主要是理解为什么这样就是答案。
其实每一个节点的贡献,就是路径的长度,路径的长度表示它被比较了多少次。
一直都是和它的祖先比较。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset> const int maxn = 1e5 + ;
vector<int>G[maxn];
int findmin(int len) {
if (len == ) return ;
if (len == ) return ;
return (len - ) * ;
}
LL findmax(int len) {
return 1LL * len * (len - ) / ;
}
struct node {
int cnt, deep;
node(int a, int b) : cnt(a), deep(b) {}
};
queue<struct node> que;
int val;
int ans[maxn];
void show(int id) {
if (G[id].size() == ) {
ans[id] = val;
// cout << val << " ";
++val;
return;
}
show(G[id][]);
ans[id] = val;
// cout << val << " ";
++val;
if (G[id].size() == ) {
show(G[id][]);
}
}
int check(vector<int> arr) {
if (arr.size() <= ) return ;
vector<int> les;
vector<int> big;
for (int i = ; i < arr.size(); ++i) {
if (arr[i] > arr[]) big.push_back(arr[i]);
else les.push_back(arr[i]);
}
return check(les) + check(big) + arr.size() - ;
}
int len, c;
void bfs(int cur) {
queue<int>que;
que.push(cur);
// vector<int>t;
while (!que.empty()) {
int now = que.front();
que.pop();
cout << ans[now] << " ";
// t.push_back(ans[now]);
for (int i = ; i < G[now].size(); ++i) {
que.push(G[now][i]);
}
}
// if (check(t) != c) {
// cout << "NO" << endl;
// } else cout << "YES" << endl;
}
int deep[maxn];
void work() {
cin >> len >> c;
if (c > findmax(len)) {
cout << - << endl;
return;
}
LL cur = findmax(len);
for (int i = ; i <= len; ++i) {
G[i].clear();
if (i != len) {
G[i].push_back(i + );
}
deep[i] = i - ;
}
while (!que.empty()) que.pop();
que.push(node(, ));
int impo = ;
for (int i = len; i >= && cur > c; --i) {
struct node t = que.front();
int one = i - t.deep - ;
int to = min(cur - c, one * 1LL);
if (to <= ) {
cout << - << endl;
return;
}
if (one == to) {
G[t.cnt].push_back(i);
deep[i] = deep[t.cnt] + ;
G[i].clear();
G[i - ].clear();
if (G[t.cnt].size() == ) {
if (t.cnt == impo) impo++;
que.pop();
que.push(node(G[t.cnt][], t.deep + ));
que.push(node(G[t.cnt][], t.deep + ));
}
} else {
G[i].clear();
G[i - ].clear();
for (int j = ; j <= len; ++j) {
if (deep[i] == deep[j] + to + ) {
G[j].push_back(i);
break;
}
}
break;
}
cur -= to;
}
val = ;
show();
bfs();
// vector<int> t;
// for (int i = 1; i <= len; ++i) {
// cout << ans[i] << " ";
// t.push_back(ans[i]);
// }
// cout << "****" << check(t);
cout << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
Lena Sort 构造题,很多细节的模拟的更多相关文章
- hdu4671 Backup Plan ——构造题
link:http://acm.hdu.edu.cn/showproblem.php?pid=4671 其实是不难的那种构造题,先排第一列,第二列从后往前选. #include <iostrea ...
- BZOJ 3097: Hash Killer I【构造题,思维题】
3097: Hash Killer I Time Limit: 5 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 963 Solved: 36 ...
- CF1110E Magic Stones(构造题)
这场CF怎么这么多构造题…… 题目链接:CF原网 洛谷 题目大意:给定两个长度为 $n$ 的序列 $c$ 和 $t$.每次我们可以对 $c_i(2\le i<n)$ 进行一次操作,也就是把 $c ...
- CodeForces 297C Splitting the Uniqueness (脑补构造题)
题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...
- cf251.2.C (构造题的技巧)
C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- Codeforces 482 - Diverse Permutation 构造题
这是一道蛮基础的构造题. - k +(k - 1) -(k - 2) 1 + k , 1 , k , 2, ....... ...
- CDOJ 1288 旅游的Final柱 构造题
旅游的Final柱 题目连接: http://acm.uestc.edu.cn/#/problem/show/1288 Description 柱神要去打Final啦~(≧▽≦)/~啦啦啦 柱神来到了 ...
- HDU 5355 Cake (WA后AC代码,具体解析,构造题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- vue 手风琴组件
1.创建组件 SqueezeBox.vue <!-- 手风琴(三级折叠列表) 组件 --> <template> <div class="header" ...
- C# LINQ Unity 单例
C# LINQ 1. 自定义 Master,Kongfu 类 1 class Master 2 { 3 4 public int Id { get; set; } 5 public string ...
- Android签名机制之---签名验证过程具体解释
一.前言 今天是元旦,也是Single Dog的嚎叫之日,仅仅能写博客来祛除寂寞了,今天我们继续来看一下Android中的签名机制的姊妹篇:Android中是怎样验证一个Apk的签名. 在前一篇文章中 ...
- visio 2010 修改 默认字体 字号大小 方法[整理]
[转自]http://www.cnblogs.com/vegaliming/archive/2012/08/09/2630568.html 1.新建一个模具 2.将常用的图形放到这个模具中 3.对每个 ...
- mysql 时间间隔计算
业务需求说明 早8:30点执行该sql,写入定时脚本 [在稳定前,需要手动复检] INSERT INTO test_error ( url, no_open_times, no_ad_times, o ...
- window下安装多个tomcat
解压该压缩包,生成3分tomcat 分别命名为 tomcat1,tomcat2,tomcat3 进入tomcat1/conf/目录,修改server.xml 进入tomcat1/bin目录,修改 se ...
- BZOJ_3448_[Usaco2014 Feb]Auto-complete_Trie树
BZOJ_3448_[Usaco2014 Feb]Auto-complete_Trie Description Bessie the cow has a new cell phone and enjo ...
- 【SCOI 2005】 扫雷
[题目链接] 点击打开链接 [算法] 只要第一行第一个数确定了,后面的数也都确定了 递推两遍即可 [代码] #include<bits/stdc++.h> using namespace ...
- 将json文件转换成insert语句的sql文件
引入是要的maven依赖: <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <depend ...
- 【转载】SQL面试题
[本文转自]http://blog.csdn.net/u012467492/article/details/46790205 1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name ...