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. PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】

    题目链接:http://www.patest.cn/contests/pat-a-practise/1001 题面: 1001. A+B Format (20) Calculate a + b and ...

  2. HDU 4897 Little Devil I 树链剖分+线段树

    Little Devil I Problem Description There is an old country and the king fell in love with a devil. T ...

  3. 正向代理tinyproxy使用总结

    使用tinyproxy的问题背景: 其实以前代理一直用的是apache,后来,那次有个任务要给ios的推送设置代理,任务很紧急,可是apache报错. 原因如下:APNS发送通知的端口2195,但是A ...

  4. Java中会存在内存泄露吗?请简单描述。

    本文转载自:Java中会存在内存泄漏吗,请简单描述 会.java导致内存泄露的原因很明确:长生命周期的对象持有短生命周期对象的引用就很可能发生内存泄露,尽管短生命周期对象已经不再需要,但是因为长生命周 ...

  5. Element is not clickable at point SeleniumWebdriverException

    Element is not clickable at point SeleniumWebdriverException | Selenium Easy http://www.seleniumeasy ...

  6. 深入理解Java执行时数据区

    前情回想 在本专栏的前12篇博客中. 我们主要大致介绍了什么是JVM, 而且具体介绍了class文件的格式. 对于深入理解Java, 或者深入理解运行于JVM上的其它语言, 深入理解class文件格式 ...

  7. 有关MAC、PHY和MII

    这是一篇转载,原文链接:http://www.cppblog.com/totti1006/archive/2008/04/22/47829.html 以太网(Ethernet)是一种计算机局域网组网技 ...

  8. NDK编程中如何在C文件中打印调试信息

      1,在Android.mk文件中加上 LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog LOCAL_PATH := $(call my-dir)include ...

  9. Ubuntu系统下实现Android工程调用独立编译的C++程序和GMP、PBC库

    目的: 实现使用C++编写代码供Android工程调用.C++代码中可以使用STL库,也可以使用常用的由源码编译生成的库,如PBC.因为PBC是基于GMP库的,所以这里只记录了GMP和PBC库的编译安 ...

  10. javascript来实现详细时间提醒信息效果

    我们经常在社交网络上面看到很人性化的时间提示比如,你的朋友几分钟前更新了什么,你的朋友几天前更新了什么信息. 这些小tips比直接显示某年某月人性化很多.我们可以用不同的程序实现这种效果.中国音效网下 ...