[HDU5592] ZYB's Premutation

题目大意:一个由\([1,n]\)组成的数列,但不知道具体排列,但给出每个前缀的逆序对数目,让你还原排列

Solution

创造一颗\([1,n]\)的权值线段树,初始权值都为\(1\),我们从后往前离线处理,每次拿到一个前缀的逆序对数\(p[i]\),说明在\(i\)上的这个数字\(a_i\)前面有\(sum=p[i]-p[i-1]\)个数字比它大,所以\(a_i\)是\(a_1,\cdots ,a_i\)中第\(i -sum\)大的数字,查询后这个\(a_i\)对前面就没有用了,需要在权值线段树上删去

Code

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #define sc(x) scanf("%d", &x)
  6. #define pf(x) printf("%d\n",x)
  7. #define lson cur << 1
  8. #define rson (cur << 1) | 1
  9. const int N = 50005;
  10. int num[N << 2], val[N << 2];
  11. int p[N], ans[N];
  12. int n;
  13. void build(int cur, int l, int r){
  14. num[cur] = r - l + 1;
  15. if(l == r){
  16. val[cur] = l;
  17. return;
  18. }else{
  19. int mid = l + ((r - l) >> 1);
  20. build(lson, l, mid);
  21. build(rson, mid + 1, r);
  22. }
  23. }
  24. int query(int cur, int l, int r, int k){
  25. int mid = l + ((r - l) >> 1);
  26. if(l == r){
  27. // pf(val[cur]);
  28. return val[cur];
  29. }else{
  30. if(num[lson] >= k){
  31. return query(lson, l, mid, k);
  32. }else{
  33. return query(rson, mid + 1, r, k - num[lson]);
  34. }
  35. }
  36. }
  37. void update(int cur, int l, int r, int k){
  38. num[cur]--;
  39. if(l == r){
  40. val[cur] = 0;
  41. return;
  42. }else{
  43. int mid = l + ((r - l) >> 1);
  44. if(k <= mid){
  45. update(lson, l, mid, k);
  46. }else{
  47. update(rson, mid + 1, r, k);
  48. }
  49. }
  50. }
  51. int main(){
  52. int t;
  53. scanf("%d", &t);
  54. while(t--){
  55. scanf("%d", &n);
  56. memset(num, 0, sizeof(num));
  57. memset(val, 0, sizeof(val));
  58. build(1, 1, n + 1);//l和r要和数组对应起来,不能建的时候是n+1,query和update时又变成了n
  59. for(int i = 1, la; i <= n; ++i){
  60. scanf("%d", &p[i]);
  61. }
  62. for(int i = n; i; --i){
  63. ans[i] = query(1, 1, n + 1, i - p[i] + p[i - 1]);
  64. update(1, 1, n + 1, ans[i]);
  65. }
  66. for(int i = 1; i < n; ++i){
  67. printf("%d ", ans[i]);
  68. }
  69. printf("%d\n", ans[n]);
  70. }
  71. return 0;
  72. }

Error

  • \(l\)和\(r\)要和数组对应起来,不能建的时候是\(n+1\),\(query\)和\(update\)时又变成了\(n\)

[HDU5592] ZYB's Premutation的更多相关文章

  1. ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...

  2. ZYB's Premutation(有逆序数输出原序列,线段树)

    ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. BestCoder Round #65 (ZYB's Premutation)

    ZYB's Premutation Accepts: 220 Submissions: 983 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  4. hdu 5592 ZYB's Premutation (权值线段树)

    最近在线段树的世界里遨游,什么都能用线段树做,这不又一道权值线段树了么. ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  5. HDU 5592——ZYB's Premutation——————【线段树单点更新、单点查询】

    ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. 线段树 - ZYB's Premutation

    ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ...

  7. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  8. ZYB's Premutation POJ5592

    Problem Description ZYBZYBZYB has a premutation PPP,but he only remeber the reverse log of each pref ...

  9. HDU 5592 ZYB's Premutation

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5592 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

随机推荐

  1. NIST随机数测试软件安装与使用 && igamc:UNDERFLOW

    https://csrc.nist.gov/ https://csrc.nist.gov/projects/random-bit-generation/documentation-and-softwa ...

  2. 解决宝塔面板没有命令行问题 && 查看宝塔面板项目环境

    # 宝塔面板没有命令行,无法查看错误输出 利用ssh.比如xshell,MObaxtern .输入ip,username,password就可以进入服务器的命令行. # 查看项目的环境 服务器默认的p ...

  3. HDU 4746 Mophues(莫比乌斯反演)题解

    题意: \(Q\leq5000\)次询问,每次问你有多少对\((x,y)\)满足\(x\in[1,n],y\in[1,m]\)且\(gcd(x,y)\)的质因数分解个数小于等于\(p\).\(n,m, ...

  4. webpack remove console.log

    webpack remove console.log https://stackoverflow.com/questions/41040266/remove-console-logs-with-web ...

  5. front-end & web & best code editor

    front-end & web & best code editor 2019 VS Code https://designrevision.com/best-code-editor/ ...

  6. Cookie 政策

    Cookie 政策 合规/隐私协议 https://www.synology.cn/zh-cn/company/legal/cookie_policy Cookie Cookie 政策 生效日期:20 ...

  7. 扫码登录 & 实现原理

    扫码登录 & 实现原理 二维码扫描登录是什么原理? https://time.geekbang.org/dailylesson/detail/100044032 xgqfrms 2012-20 ...

  8. scrimba & interactive free online tutorials

    scrimba & interactive free online tutorials https://github.com/scrimba/community/blob/master/FAQ ...

  9. js showOpenFilePicker showSaveFilePicker showDirectoryPicker API

    选择文件,获取文件句柄 btn.addEventListener("click", async (e) => { try { const hFiles = await win ...

  10. nasm astrchr函数 x86

    xxx.asm: %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export as ...