记录编号 73235 评测结果 AAAAAAAAAAA
题目名称 数对的个数 最终得分 100
用户昵称 Gravatarranto 是否通过 通过
代码语言 C++ 运行时间 0.080 s
提交时间 2013-10-20 21:22:31 内存使用 0.31 MiB
显示代码纯文本
  1. #include <fstream>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <climits>
  6.  
  7. using namespace std;
  8.  
  9. ifstream in("dec.in");
  10. ofstream out("dec.out");
  11.  
  12. struct node
  13. {
  14. node(long i,long n):index(i),num(n){}
  15. long index;
  16. long num;
  17. };
  18.  
  19. node make(long i,long n)
  20. {
  21. node ret(i,n);
  22. return ret;
  23. }
  24.  
  25. long n,c;
  26. long *f;
  27. long ans(0);
  28.  
  29. void input();
  30. void proc();
  31. void output();
  32.  
  33. int main()
  34. {
  35. input();
  36. proc();
  37. output();
  38. return 0;
  39. }
  40.  
  41. void input()
  42. {
  43. in>>n>>c;
  44. f=new long [n];
  45. for(int i=0;i!=n;++i)
  46. {
  47. in>>f[i];
  48. }
  49. return ;
  50. }
  51.  
  52. void proc()
  53. {
  54. if(n==1)
  55. {
  56. return ;
  57. }
  58. vector<node> nvector;
  59. nvector.reserve(n);
  60. stable_sort(f,f+n);
  61. long comp(f[0]);
  62. long num(0);
  63. //long j(0);
  64. for(int i=0;i!=n;++i)
  65. {
  66. if(f[i]==comp)
  67. {
  68. ++num;
  69. continue;
  70. }
  71. if(f[i]>comp)
  72. {
  73. nvector.push_back(make(comp,num));
  74. //out<<nvector[j].index<<' '<<nvector[j].num<<endl;
  75. //++j;
  76. comp=f[i];
  77. num=1;
  78. }
  79. }
  80. nvector.push_back(make(comp,num));
  81. //out<<nvector[j].index<<' '<<nvector[j].num<<endl;
  82. unsigned long i(0);
  83. #define nv nvector
  84. for(unsigned long j=0;j!=nv.size();)
  85. {
  86. if(nv[j].index-nv[i].index==c)
  87. {
  88. ans+=nv[j].num*nv[i].num;
  89. ++i;
  90. ++j;
  91. continue;
  92. }
  93. if(nv[j].index-nv[i].index<c)
  94. {
  95. ++j;
  96. continue;
  97. }
  98. if(nv[j].index-nv[i].index>c)
  99. {
  100. ++i;
  101. continue;
  102. }
  103. }
  104. return ;
  105. }
  106.  
  107. void output()
  108. {
  109. out<<ans<<endl;
  110. return ;
  111. }
  112.  
  113. /* if(f[i]>comp)
  114. {
  115. nlist.push_back(make(comp,num));
  116. out<<(*(nlist.end()-1)).index<<' '<<(*(nlist.end()-1)).num<<endl;
  117. if(comp-lmin==c)
  118. {
  119. node ntemp(nlist.front());
  120. ans+=ntemp.num*num;
  121. nlist.pop_front();
  122. lmin=nlist.front().index;
  123. }
  124. if(comp-lmin>c)
  125. {
  126. nlist.pop_front();
  127. lmin=nlist.front().index;
  128. }
  129. comp=f[i];
  130. num=1;
  131. }
  132. */