比赛 近5年noip/csp题目回顾 评测结果 WWWWWWAWAWWWWWWWWWWW
题目名称 最小环(民间数据) 最终得分 10
用户昵称 ┭┮﹏┭┮ 运行时间 1.295 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2022-06-27 11:00:45
显示代码纯文本
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 200010;
  4. long long s;
  5. int n,m;
  6. int a[N*2],k;
  7. int main(){
  8. freopen("noi_online2020_ring.in","r",stdin);
  9. freopen("noi_online2020_ring.out","w",stdout);
  10. cin>>n>>m;
  11. for(int i = 1;i <= n;i++){
  12. cin>>a[i];
  13. }
  14. sort(a+1,a+1+n);
  15. for(int i = 1;i <= m;i++){
  16. s = 0;
  17. cin>>k;
  18. if(k == 0){
  19. for(int j = 1;j <= n;j++){
  20. s += a[j] * a[j];
  21. }
  22. }
  23. else if(k == 1){
  24. s = a[n] * a[n-1] + a[1] * a[2];
  25. for(int j = n;j >= 3;j--){
  26. s += a[j] * a[j-2];
  27. }
  28. }
  29. else if(k == 2){
  30. if(n % 2 == 0){
  31. int l = n / k;
  32. for(int j = n;j >= 1;j-=l){
  33. for(int o = j;o >= j-l+2;o--){
  34. s += a[o] * a[o-1];
  35. }
  36. s += a[j] * a[j-l+1];
  37. }
  38. }
  39. else{
  40. for(int j = n;j >= 3;j--){
  41. s += a[j] * a[j-2];
  42. }
  43. s += a[n] * a[n-1];
  44. }
  45. }
  46. cout<<s<<endl;
  47. }
  48. return 0;
  49. }