记录编号 156091 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [国家集训队2011]种树 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.250 s
提交时间 2015-04-02 10:45:55 内存使用 2.80 MiB
显示代码纯文本
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <queue>
  9. #include <stack>
  10. #include <map>
  11. #include <set>
  12. #include <list>
  13. #include <vector>
  14. #include <ctime>
  15. #include <functional>
  16. #define pritnf printf
  17. #define scafn scanf
  18. #define sacnf scanf
  19. #define For(i,j,k) for(int i=(j);i<=(k);(i)++)
  20. #define Clear(a) memset(a,0,sizeof(a))
  21. using namespace std;
  22. typedef unsigned int Uint;
  23. const int INF=0x7fffffff;
  24. const double eps=1e-10;
  25. ///==============struct declaration==============
  26. struct Node{
  27. int x,val;
  28. Node (int x=0,int val=0):x(x),val(val){}
  29. bool operator <(const Node &rhs) const{
  30. return val<rhs.val;
  31. }
  32. };
  33. ///==============var declaration=================
  34. const int MAXN=200010;
  35. int n,ans,m;
  36. int w[MAXN];
  37. int prev[MAXN],next[MAXN];
  38. bool del[MAXN];
  39. priority_queue <Node> Q;
  40. ///==============function declaration============
  41.  
  42. ///==============main code=======================
  43. int main()
  44. {
  45. //#define FILE__
  46. //#ifdef FILE__
  47. freopen("nt2011_tree.in","r",stdin);
  48. freopen("nt2011_tree.out","w",stdout);
  49. //#endif
  50. scanf("%d%d",&n,&m);
  51. if (m>n/2){
  52. printf("Error!\n");
  53. return 0;
  54. }
  55. for(int i=1;i<=n;i++){
  56. scanf("%d",w+i);
  57. prev[i]=i==1?n:i-1;
  58. next[i]=i==n?1:i+1;
  59. Q.push(Node(i,w[i]));
  60. }
  61. int Ans=0;
  62. while (m--){
  63. Node xx;int x;
  64. do{
  65. xx=Q.top();Q.pop();
  66. x=xx.x;
  67. }while (del[x]);
  68. Ans+=w[x];w[x]=w[prev[x]]+w[next[x]]-w[x];
  69. del[prev[x]]=del[next[x]]=true;
  70. next[prev[prev[x]]]=x;
  71. prev[x]=prev[prev[x]];
  72. prev[next[next[x]]]=x;
  73. next[x]=next[next[x]];
  74. Q.push(Node(x,w[x]));
  75. }
  76. printf("%d\n",Ans);
  77. return 0;
  78. }
  79. ///================fuction code====================