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 构造题,很多细节的模拟的更多相关文章

  1. hdu4671 Backup Plan ——构造题

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4671 其实是不难的那种构造题,先排第一列,第二列从后往前选. #include <iostrea ...

  2. BZOJ 3097: Hash Killer I【构造题,思维题】

    3097: Hash Killer I Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 963  Solved: 36 ...

  3. CF1110E Magic Stones(构造题)

    这场CF怎么这么多构造题…… 题目链接:CF原网 洛谷 题目大意:给定两个长度为 $n$ 的序列 $c$ 和 $t$.每次我们可以对 $c_i(2\le i<n)$ 进行一次操作,也就是把 $c ...

  4. CodeForces 297C Splitting the Uniqueness (脑补构造题)

    题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...

  5. cf251.2.C (构造题的技巧)

    C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...

  6. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  7. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

  8. CDOJ 1288 旅游的Final柱 构造题

    旅游的Final柱 题目连接: http://acm.uestc.edu.cn/#/problem/show/1288 Description 柱神要去打Final啦~(≧▽≦)/~啦啦啦 柱神来到了 ...

  9. HDU 5355 Cake (WA后AC代码,具体解析,构造题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...

随机推荐

  1. Android sdcard读写权限问题之中的一个

    博主在刚刚在学习过程中发现了一个关于android往sdcard读写的问题, 配置了该配置的提示无读写权限. 在AndroidManifest.xml文件里配置清单例如以下 <manifest ...

  2. hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)

    链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...

  3. Codeforces 344B Simple Molecules

    #include<bits/stdc++.h> using namespace std; int main() { int a,b,c; scanf("%d%d%d", ...

  4. JSON序列化-化繁为简

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. mongodb数据出现undefined如何查询

    某些字段出现undefined,该如何查询? 如下: db.getCollection('license').find({"holder_code":{$type:"un ...

  6. c中的变量

    1 变量类型 1.1 static global or static .data/.bss 1.2 automic stack,its relevant to os kernel and compil ...

  7. publish and submit

    http://blog.csdn.net/w_jewelry/article/details/8123639 1.Gerrit里点击“publish and submit”提示如下:Your chan ...

  8. bzoj 4247: 挂饰【dp】

    bzoj上访问负下标会跑到奇怪的地方-- 其实可以滚动数组优化,但是我看能过就懒得改了 设f[i][j]为已经算了前i个挂饰,当前有j个空的钩子,转移就是f[i][j]=max(f[i-1][j],f ...

  9. 身份认证系统(四)OAuth2运行流程

    上一节介绍过什么是OAuth2,这节准备用生动的事例来告诉大家OAuth2运行的流程. 我们来想这样一个场景:假设我们有一个叫做万方网盘的服务是用来帮助用户存储论文文档的,我们向外提供了符合OAuth ...

  10. 初窥MySQL性能调优

    本文涉及:MySQL自带的性能测试工具mysqlslap的使用及几个性能调优的方法 性能测试工具—mysqlslap mysqlslap是MySQL自带的一款非常优秀的性能测试工具.使用它可以 模拟多 ...