hdu 6237
来源:互联网 发布:网络基础技术视频教程 编辑:程序博客网 时间:2024/06/05 00:58
(贪心)
题意:给定
思路:依题意可知,
吐槽:题目难点在于如何贪心,如果同时考虑拿走多少个石子和需要补多少个石子,就会想复杂。所以要简化条件,只考虑一方面就可以求解。
代码:
#include <cstdio>#include <vector>#include <cstring>#include <algorithm>#define LL long longusing namespace std;const int maxn = 100010;const LL inf = 1000000000000;LL a[maxn], t[maxn];LL fac[maxn], cnt;void get_fac(LL x) { LL t = x; cnt = 0; for(LL i=2; i*i<=t; i++) if(t%i == 0) { while(t%i == 0) t /= i; fac[cnt++] = i; } if(t > 1) fac[cnt++] = t;}int main() { LL T, n; scanf("%lld",&T); while(T --) { scanf("%lld",&n); LL sum = 0; for(LL i=1; i<=n; i++) { scanf("%lld",&a[i]); sum += (LL)a[i]; } sort(a+1, a+n+1); get_fac(sum); LL ans = inf; for(LL i=0; i<cnt; i++) { LL x = fac[i], res = 0, all = 0; for(LL j=1; j<=n; j++) { t[j] = a[j] % x; all += t[j]; } sort(t+1, t+n+1); for(LL j=n; j>=1; j--) if(all > 0) { res += x-t[j]; all -= x; } ans = min(ans, res); //printf("x:%lld res:%lld\n",x,res); } printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- hdu 6237
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- hdu 找新朋友
- javascript换行问题
- 数字图像处理,初识压缩感知Compressive Sensing
- cisco静态浮动路由详细配置
- Java实例-九九乘法表
- hdu 6237
- Python基础 访问网络资源 requests
- Oracle11g安装教程图解
- 2017.12.20练习
- Intel DPDK部署
- 创建一个Rectangle类,添加winth和height两个成员变量.........
- 解决 tensorflow softmax_cross_entropy_with_logits() 报错 Only call `softmax_cross_entropy_with_logits`
- Python文本数据处理
- 进程和线程关系及区别