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) ...
随机推荐
- Deepin-安装和卸载软件
一般默认厂商源安装软件 安装软件: 示例:sudo apt-get install xx 实例:sudo apt-get install nodejs 卸载软件: 示例:sudo apt-get -- ...
- Nerv --- React IE8 兼容方案
创建项目 创建一个目录,使用npm快速初始化 $ mkdir my-project && npm init -y 安装依赖 安装webpack以及babel $ npm install ...
- iOS开发-UITableView单选多选/复选实现1
TableView怎样实现单选或者多选呢? 我们的直接思路是改动某一个Cell的样式就可以, 那么改动样式须要通过改动相应的数据, 从这里能够判断我们须要给Cell相应的数据设置一个标志位, 当选中的 ...
- (void __user *)arg 中__user的作用
__user宏简单告诉编译器(通过 noderef)不应该解除这个指针的引用(因为在当前地址空间中它是没有意义的). (void __user *)arg 指的是arg值是一个用户空间的地址,不能直接 ...
- 3.将maven项目jar纳入maven仓库,Mave项目依赖另外一个Maven项目的案例
1 若想让maven项目依赖另外一个maven项目.被依赖的项目要在maven仓库中有对应的jar包,所以要对依赖的项目运行mvninstall命令. 2 新建第二个项目模块HelloFrien ...
- 在matlab中生成m序列
实验环境为matlab2013b 1.首先编写一个mseq.m文件,内容为: function[mseq]=m_sequence(fbconnection) n=length(fbconnectio ...
- Sql sever 分组排序
维护人事的时候人事局要求加入一个新功能,详细需求例如以下:加入的人员在同一个单位的依照顺序编号而且单位也要实现时间排序,也就是说有两个排序,第一单位名称排序.先创建的一直在前.然后依照创建时间依次排序 ...
- 【Sublime】Sublime Text 2集成TortoiseSVN插件
作者:zhanhailiang 日期:2014-09-30 1. 下载TortoiseSVN.将其安装在默认位置: 2. 使用Sublime包管理器下载安装TortoiseSVN Plugin,安装后 ...
- 【bzoj1965】[Ahoi2005]SHUFFLE 洗牌
x*2^m==l (mod n+1)x=(n/2+1)^m*l mod n+1 #include<algorithm> #include<iostream> #include& ...
- HDU1257 最少拦截系统 —— 贪心
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Othe ...