题目:

小O是一个热爱短代码的选手。在缩代码方面,他是一位身经百战的老手。世界各地的OJ上,很多题的最短解答排行榜都有他的身影。这令他感到十分愉悦。

最近,他突然发现,很多时候自己的程序明明看起来比别人的更短,实际代码量却更长。这令他感到很费解。经过一番研究,原来是因为他每一行的缩进都全是由空格组成的,大量的空格让代码量随之增大。

现在设小O有一份 \(n\) 行的代码,第 \(i\) 行有 \(a_i\) 个空格作为缩进。

为解决这一问题,小O要给自己文本编辑器设定一个正整数的默认TAB宽度 \(x\),然后对于每一行,编辑器从头至尾不断把连续 \(x\) 个空格替换成一个TAB,直到剩余空格数不足 \(x\) 个。

最终缩进所占代码量为空格数与TAB数的和。请你帮小O选择一个合适的 \(x\),使得缩进所占代码量最小。

题解:

我们容易发现对于一个确定的\(x\),答案即为\(\sum_{i=1}^n[\frac{a_i}{x} + (a_i \mod x)]\)

因为有一个模运算,所以我们不好求值.

我们考虑去掉这个模运算.我们考虑计算每个转化可以减少多少代码量.

对于一个\(siz\)为\(x\)的的TAB转化,可以减少\(x-1\)的代码量.

所以我们发现对于一个确定的x,可以减少的代码量为\((x-1)\sum_{i=1}^n[\frac{a_i}{x}]\)

然后我们考虑枚举,

首先我们枚举\(x\),然后枚举\([\frac{a_i}{x}]\)的值

然后可以利用桶统计在\([x*\frac{a_i}{x},x*(\frac{a_i}{x}+1))\)内的数的个数

更新答案即可.

容易发现这是\(O(nlogn)\)的.

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6. #define rg register int
  7. inline void read(int &x){
  8. x=0;char ch;bool flag = false;
  9. while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
  10. while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
  11. }
  12. const int maxn = 1000010;
  13. int c[maxn];
  14. int main(){
  15. int n;read(n);
  16. int m = 0;ll ans = 0;
  17. for(rg i=1,x;i<=n;++i){
  18. read(x);m = max(m,x);
  19. ++ c[x];ans += x;
  20. }
  21. for(rg i=1;i<=m;++i) c[i] += c[i-1];
  22. ll del = 0,res = 0;
  23. for(rg x = 2;x <= m;++x){
  24. res = 0;
  25. for(rg y=1;y <= (m/x);++y){
  26. res += 1LL*y*(c[min(x*(y+1)-1,m)] - c[x*y-1]);
  27. }
  28. del = max(del,res*(x-1));
  29. }
  30. printf("%lld\n",ans-del);
  31. return 0;
  32. }

uoj problem 21 缩进优化的更多相关文章

  1. ●UOJ 21 缩进优化

    题链: http://uoj.ac/problem/21 题解: ...技巧题吧 先看看题目让求什么: 令$F(x)=\sum_{i=1}^{n}(\lfloor a[i]/x \rfloor +a[ ...

  2. UOJ#21 【UR #1】缩进优化

    传送门 http://uoj.ac/problem/21 枚举 (调和级数?) $\sum_{i=1}^{n} (a_i / x + a_i \bmod x) =\sum a_i - (\sum_{i ...

  3. UOJ_21_【UR #1】缩进优化_数学

    UOJ_21_[UR #1]缩进优化_数学 题面:http://uoj.ac/problem/21 最小化$\sum\limits{i=1}^{n}a[i]/x+a[i]\;mod\;x$ =$\su ...

  4. uoj problem 10

    uoj problem 10 题目大意: 给定任务若干,每个任务在\(t_i\)收到,需要\(s_i\)秒去完成,优先级为\(p_i\) 你采用如下策略: 每一秒开始时,先收到所有在该秒出现的任务,然 ...

  5. (Problem 21)Amicable numbers

    Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into ...

  6. Problem 21

    Problem 21 https://projecteuler.net/problem=21 Let d(n) be defined as the sum of proper divisors of  ...

  7. 【UOJ#21】【UR#1】缩进优化

    我好弱啊,什么题都做不出来QAQ 原题: 小O是一个热爱短代码的选手.在缩代码方面,他是一位身经百战的老手.世界各地的OJ上,很多题的最短解答排行榜都有他的身影.这令他感到十分愉悦. 最近,他突然发现 ...

  8. 【uoj#21】[UR #1]缩进优化 数学

    题目描述 给出 $n$ 个数 ,求 $\text{Min}_{x=1}^{\infty}\sum\limits_{i=1}^n(\lfloor\frac {a_i}x\rfloor+a_i\ \tex ...

  9. HDU 2993 MAX Average Problem(斜率优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 Problem Description Consider a simple sequence w ...

随机推荐

  1. obj-c学习笔记

    本文转载至 http://blog.csdn.net/c395565746c/article/details/7573793   当对象经过在dealloc方法处理时,该对象就已经处于已销毁状态,其它 ...

  2. [原创]将本地代码共享到github的操作步骤

    将本地代码共享到github的操作步骤 本地代码目录执行如下命令,初始化为git仓库. git init 到github上新建一个仓库,假设为https://github.com/sky0014/sk ...

  3. Django之stark组件的使用和总结

    Stark组件的使用 组件的字段 list_display=[] 需要显示的字段 list_display_links=[] #需要链接编辑字段 Stark_Model_Form=[] #设置Mode ...

  4. SQL2000 3核6核 CUP 安装SP4

    1.找到HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432node\Microsoft\MSSQLServer \MSSQLServer\Parameters\ 2.然后加入下面的 ...

  5. Windows平台下搭建Git服务器的图文教程(转发)

    Git没有客户端服务器端的概念,但是要共享Git仓库,就需要用到SSH协议(FTP , HTTPS , SFTP等协议也能实现Git共享,此文档不讨论),但是SSH有客户端服务器端,所以在window ...

  6. QT设置textEdit光标到末尾

    //移动光标到末尾 QTextCursor cursor = ui->receiveTextEdit->textCursor(); cursor.movePosition(QTextCur ...

  7. 关于随机浏览头伪装fake-UserAgent

    使用: from fake_useragent import UserAgent ua = UserAgent() #ie浏览器的user agent print(ua.ie) Mozilla/5.0 ...

  8. migrate

    数据类型 引用 # :string, :text, :integer, :float,:decimal, :datetime, :timestamp, :time, :date, # :binary, ...

  9. 【leetcode刷题笔记】Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  10. 0428 正则表达式 re模块

    复习 异常处理try except 一定要在except之后写一些提示或者处理的内容 try: '''可能会出现异常的代码'''except ValueError: '''打印一些提示或者处理的内容' ...