\(f(l,r,k)=\)区间[\(l\),\(r\)]的第k大。

\(\sum_{l=1}^{n}{\sum_{r=l}^{n}{f(l,r,k)}}\)

参考题解,claris大佬题解。赛后AC。。。

题目链接

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <string>
  7. using namespace std;
  8. typedef long long int LL;
  9. const int MOD = 1e9 + 7;
  10. const int INF = 2e9 + 1e8;
  11. const int maxn = 1e6 + 100;
  12. struct dsajkbdsuhvjgfdwsaghvfewdsjhfcvdsghvfdsjhfbcgvdsgh
  13. {
  14. int pre,next,val;
  15. }lst[maxn];
  16. int pos[maxn],n,k;;
  17. int temp[maxn];
  18. void del(int x)
  19. {
  20. int pre=lst[x].pre,nxt=lst[x].next;
  21. lst[pre].next=nxt;
  22. lst[nxt].pre=pre;
  23. }
  24. LL calc(int x)
  25. {
  26. int lft[100],rit[100];
  27. int il=0,ir=0;
  28. for(int i=x;i>0;i=lst[i].pre)
  29. {
  30. lft[++il]=i-lst[i].pre;
  31. if(il==k) break;
  32. }
  33. for(int i=x;i<=n;i=lst[i].next)
  34. {
  35. rit[++ir]=lst[i].next-i;
  36. if(ir==k) break;
  37. }
  38. LL res=0;
  39. for(int i=1;i<=il;i++)
  40. {
  41. if(k-i+1<=ir) res+=1ll*lft[i]*rit[k-i+1];
  42. }
  43. return res;
  44. }
  45. int main()
  46. {
  47. int ncase;
  48. scanf("%d",&ncase);
  49. while(ncase--)
  50. {
  51. scanf("%d%d",&n,&k);
  52. for(int i=1;i<=n;i++)
  53. {
  54. lst[i].pre=i-1,lst[i].next=i+1;
  55. scanf("%d",&lst[i].val);
  56. pos[lst[i].val]=i;
  57. }
  58. lst[0].next=1,lst[n+1].pre=n;
  59. LL sum=0;
  60. for(int i=1;i<=n-k+1;i++)
  61. {
  62. sum+=i*calc(pos[i]);
  63. del(pos[i]);
  64. }
  65. cout<<sum<<endl;
  66. }
  67. return 0;
  68. }

hdu 6058的更多相关文章

  1. HDU 6058 - Kanade's sum | 2017 Multi-University Training Contest 3

    /* HDU 6058 - Kanade's sum [ 思维,链表 ] | 2017 Multi-University Training Contest 3 题意: 给出排列 a[N],求所有区间的 ...

  2. HDU - 6058 Kanade's sum

    Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6058 /* 思路是:找出每个x为第k大的区间个数有多少 用pos[i]保存当前x的位置, ...

  3. 【链表】2017多校训练三 HDU 6058 Kanade's sum

    acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...

  4. hdu 6058 思维

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6058 分析题目的时候,由于枚举的区间很多,而第k大的值范围小,应该要想到去枚举第k大的值然后找到这个值对答 ...

  5. 2017ACM暑期多校联合训练 - Team 3 1003 HDU 6058 Kanade's sum (模拟)

    题目链接 Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th largest elem ...

  6. hdu 6058 Kanade's sum(模拟链表)

    Kanade's sum Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  7. hdu 6058 Kanade's sum (计算贡献,思维)

    题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和 思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o( ...

  8. HDU 6058 Kanade's sum 二分,链表

    Kanade's sum Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th larg ...

  9. HDU 6058:Kanade's sum(思维)

    题目链接 题意 给出一个n和一个k,求1~n的每个区间的第k大的总和是多少,区间长度小于k的话,贡献为0. 思路 首先有一个关系:当一个数是第k大的时候,前面有x个比它大的数,那么后面就有k-x-1个 ...

随机推荐

  1. iOS -- xxxViewController进行pop时直接crash进main.m,EXC_BAD_ACCESS(code=1,address=0x20)

    今天在调试程序时,遇到了奇怪的错误.我从主页跳进(push)一个ViewController时一切正常,但是返回主页(pop)时却crash了,直接跳进了main.m(EXC_BAD_ACCESS(c ...

  2. 彻底搞懂oracle的标量子查询

    oracle标量子查询和自己定义函数有时用起来比較方便,并且开发者也常常使用.数据量小还无所谓.数据量大,往往存在性能问题. 下面測试帮助大家彻底搞懂标量子查询. SQL> create tab ...

  3. Cucumber+Rest Assured快速搭建api自动化测试平台

    转载:http://www.jianshu.com/p/6249f9a9e9c4 什么是Cucumber?什么是BDD?这里不细讲,不懂的直接查看官方:https://cucumber.io/ 什么是 ...

  4. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  5. 英特尔和Red Hat合作实现Gnome桌面的Wayland支持

    在发布支持XMir的Linux图形驱动程序xf86-video-intel 2.99.901后数天,英特尔宣布撤回对XMir的支持,XMir补丁不会合并到上游项目.XMir是Mir显示服务器的X11兼 ...

  6. oracle ORA-06550

    declare  cnt integer; begin     select count(0)     into cnt     from user_all_tables    where table ...

  7. ActiveMQ 消息持久化到Mysql数据库

    [root@txylucky local]# tar -zxvf apache-activemq-5.15.8-bin.tar.gz[root@txylucky local]# mv apache-a ...

  8. 互联网金融MySQL优化参数标准

    InnoDB配置 从MySQL 5.5版本开始,InnoDB就是默认的存储引擎并且它比任何其它存储引擎的使用要多得多.那也是为什么它需要小心配置的原因. innodb_file_per_table 表 ...

  9. CentOS 安装和配置 Mantis

    Mantis是一个基于PHP技术的轻量级的开源缺陷跟踪系统,以Web操作的形式提供项目管理及缺陷跟踪服务.在功能上.实用性上足以满足中小型项目的管理及跟踪.更重要的是其开源,不需要负担任何费用. 1. ...

  10. Android自己定义控件--下拉刷新的实现

    我们在使用ListView的时候.非常多情况下须要用到下拉刷新的功能.为了了解下拉刷新的底层实现原理,我採用自己定义ListView控件的方式来实现效果. 实现的基本原理是:自己定义ListView, ...