D2. Equalizing by Division (hard version)

涉及下标运算一定要注意下标是否越界!!!

思路,暴力判断以每个数字为到达态最小花费

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define sc(x) scanf("%I64d",&x);
  4. #define read(A) for(int i=1;i<=n;i++)scanf("%I64d",&A[i]);
  5. #define int long long
  6. #define P pair<int,int>
  7. #define fi first
  8. #define se second
  9. #define endl '\n'
  10. #define ll long long
  11. #define maxn 200000+10
  12. int n,m,T;
  13. int A[maxn];
  14. int B[];
  15. int ch(int x,int y)
  16. {
  17. for(int i=;i<=y;i++){
  18. x/=;
  19. }
  20. return x;
  21. }
  22. int Ans=1e18;
  23. int check(int x,int t)
  24. {
  25. int _x=x;
  26. if(x==)
  27. {
  28. int ans=;
  29. for(int i=B[]; i<n; i++)
  30. {
  31. int c=A[i];
  32.  
  33. while(c)
  34. {
  35. ans++;
  36. c/=;
  37. if(ans>Ans){
  38. return 1e18;
  39. }
  40. }
  41. t++;
  42. //cout<<t<<m<<endl;
  43. if(t==m)
  44. {
  45. return ans;
  46. }
  47. }
  48. }
  49. int ans=;
  50. int k=;
  51. int y=;
  52. x*=;
  53. while(x<=&&t<m)
  54. {
  55. for(int i=; i<k; i++)
  56. {
  57. if(ch(x+i,y)!=_x)break;
  58. if(x+i>)break;
  59. if(x+i<=&&B[x+i]>=m-t)
  60. {
  61. ans+=(m-t)*y;
  62. if(ans>Ans)return 1e18;
  63. t=m;
  64. return ans;
  65. }
  66. else
  67. {
  68. ans+=(B[x+i])*y;
  69. if(ans>Ans)return 1e18;
  70. t+=B[x+i];
  71. }
  72.  
  73. }
  74. x*=;
  75. y++;
  76. k*=;
  77. }
  78. if(m<=t)return ans;
  79. else
  80. return 1e18;
  81. }
  82. signed main()
  83. {
  84. sc(n);
  85. sc(m);
  86. for(int i=; i<n; i++)
  87. {
  88. sc(A[i]);
  89. //cout<<A[i]<<endl;
  90. B[A[i]]++;
  91. if(B[A[i]]>=m)
  92. {
  93. puts("");
  94. return ;
  95. }
  96. }
  97. sort(A,A+n);
  98. int t=;
  99.  
  100. for(int i=; i<=; i++)
  101. {
  102. t=check(i,B[i]);
  103. // if(t<ans)cout<<i<<endl;
  104. Ans=min(t,Ans);
  105. }
  106. cout<<Ans<<'\n';
  107.  
  108. }

D2. Equalizing by Division (hard version)的更多相关文章

  1. codeforces Equalizing by Division (easy version)

    output standard output The only difference between easy and hard versions is the number of elements ...

  2. D2. Remove the Substring (hard version)(思维 )

    D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...

  3. D2. Remove the Substring (hard version)

    D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...

  4. CF1213D Equalizing by Division

    easy version hard version 问题分析 直接从hard version入手.不难发现从一个数\(x\)能得到的数个数是\(O(\log x)\)的.这样总共有\(O(n\log ...

  5. Codeforces 1249 D2. Too Many Segments (hard version)

    传送门 贪心 对于第一个不合法的位置,我们显然要通过删除几个覆盖了它的区间来使这个位置合法 显然删右端点更靠右的区间是更优的,所以就考虑优先删右端点靠右的,然后再考虑下一个不合法位置 用一个 $set ...

  6. codeforces 1249 D2 Too Many Segments (hard version) 贪心+树状数组

    题意 给定n个线段,线段可以相交,第\(i\)个线段覆盖的区间为\([l_i,r_i]\),问最少删除多少个线段让覆盖每个点的线段数量小于等于k. 分析 从左往右扫每个点\(x\),若覆盖点\(x\) ...

  7. Codeforces 1213D Equalizing by Division

    cf题面 中文题意 给n个数,每次可以把其中一个数字位运算右移一位(即整除以二),问要至少操作几次才能让这n个数中有至少k个相等. 解题思路 这题还有个数据范围更小的简单版本,n和k是50,\(a_i ...

  8. Equalizing by Division

    The only difference between easy and hard versions is the number of elements in the array. You are g ...

  9. Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)

    题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...

随机推荐

  1. Sqlserver限制用户访问指定数据库

    USE master CREATE LOGIN test --要创建的用户名 WITH PASSWORD = '123456', --密码 DEFAULT_DATABASE = DBTest, --指 ...

  2. 洛谷 P1879 玉米田Corn Fields 题解

    题面 一道思维难度不大的状态压缩,也并不卡常,但细节处理要格外注意: f[i][j]表示前i行最后一行状态是j的方案数 #include <bits/stdc++.h> #define p ...

  3. 如何输出opencv编译信息

    本文链接:https://mangoroom.cn/opencv/how-to-print-compile-info-of-opencv.html opencv提供了一个函数,利用这个函数可以输出当前 ...

  4. MUI沉浸式代码

    var ben_immer = (function() { var immersed = 0; var ms = (/Html5Plus\/.+\s\(.*(Immersed\/(\d+\.?\d*) ...

  5. 日语能力考试N2级核心词汇必备—副词

                                                                         日语能力考试N2级核心词汇必备—副词 ABAB型的副词 あちこ ...

  6. 85. Maximal Rectangle (JAVA)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. Assets.xcassets的使用

    先介绍下Asset Catalog,Asset Catalog是Xcode5引入的一个新的图片管理方式,有几个好处: 1.自动管理图片,如@1x,@2x图片,使用的时候使用Asset 名字即可 2.管 ...

  8. 当有多个相同的DIV时,我怎么判断我点击的是哪个

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. oracle数据库应用总结

    1------->>>>>>>>>>>>>>>>>>>>>>> ...

  10. Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...