Naughty Stone Piles
题目:http://codeforces.com/problemset/problem/227/D
题意:n堆个数石子,每堆石子有ai个,通过合并(即将一堆石子移到另一堆石子上),将所有石子合并为一堆,每次合并的代价是这堆石子的个数,在ki的限制下,每堆石子最多只能被合并k次,k次之后就必须移动该堆石子到别的堆上,求合并到最后的总的最小代价。
题解:emmmmm,既然是找最小代价,那么石子个数最多的那堆石子肯定是不移动的,把个数次小的k堆石子移动到这堆石子上,所以这k堆石子移动了一次,这k堆石子中的每一个又是比他们小的k堆石子合并到它们身上的,即这k*k堆移动了两次,以此类推:
k 1次
k*k 2次
k*k*k 3次
k*k*k*k 4次
.
.
.
.
所以总代价最小即是k*1*个数+k^2*2*个数+k^3*3*个数.....
当然k为1时需要特判,最小堆移动了n-1次,次小堆移动n-2次,最大堆不动,具体看代码。
注意大部分数据都是long long 范围。
1 #include <map>
2 #include <stack>
3 #include <queue>
4 #include <cmath>
5 #include <string>
6 #include <limits>
7 #include <cstdio>
8 #include <vector>
9 #include <cstdlib>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 #define Scc(c) scanf("%c",&c)
14 #define Scs(s) scanf("%s",s)
15 #define Sci(x) scanf("%d",&x)
16 #define Sci2(x, y) scanf("%d%d",&x,&y)
17 #define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z)
18 #define Scl(x) scanf("%I64d",&x)
19 #define Scl2(x, y) scanf("%I64d%I64d",&x,&y)
20 #define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z)
21 #define Pri(x) printf("%d\n",x)
22 #define Prl(x) printf("%I64d\n",x)
23 #define Prc(c) printf("%c\n",c)
24 #define Prs(s) printf("%s\n",s)
25 #define For(i,x,y) for(int i=x;i<y;i++)
26 #define For_(i,x,y) for(int i=x;i<=y;i++)
27 #define FFor(i,x,y) for(int i=x;i>y;i--)
28 #define FFor_(i,x,y) for(int i=x;i>=y;i--)
29 #define Mem(f, x) memset(f,x,sizeof(f))
30 #define LL long long
31 #define ULL unsigned long long
32 #define MAXSIZE 100005
33 #define INF 0x3f3f3f3f
34 const int mod=1e9+7;
35 const double PI = acos(-1.0);
36
37 using namespace std;
38 int cmp(LL a,LL b)
39 {
40 return a<b;
41 }
42 int main()
43 {
44
45 LL n;
46 Scl(n);
47 int i;
48 LL a[MAXSIZE]= {0};
49 for (i=1; i<=n; i++)
50 Scl(a[i]);
51 sort(a+1,a+1+n);
52 LL tmp=0;
53 For_(i,1,n-1)//注意这两个for循环的先后顺序
54 tmp+=a[i]*(n-i);
55 For_(i,1,n)
56 a[i]+=a[i-1];//用来计算每隔k的x次方堆石子被合并的次数是相同的,即(a[m]-a[m-k])*cnt,
57 int q;
58 Sci(q);
59 while(q--)
60 {
61 LL ans=0;
62 LL k;
63 Scl(k);
64 int t=k;
65 LL cnt=1,m=n-1;
66 if(k!=1)
67 {
68 while(m>=k)//当k大于等于n时,不执行,直接执行 ans+=cnt*a[m];,即最小代价是前n-1堆石子个数和,即1*a[m].
69 {
70 ans+=(a[m]-a[m-k])*cnt;
71 cnt++;
72 m-=k;
73 k=k*t;//k的变化是k k^2 k^3 k^4...每次乘以最初的k的大小,刚开始写成k*=k,wa一下午,我太难了QAQ
74 }
75 ans+=cnt*a[m];
76 }
77 else
78 ans=tmp;
79 printf("%I64d ",ans);//还有这个谜之输出。
80 }
81 return 0;
82 }
嗯,本来不是难题,硬是用了我一天的时间,真是。。。。。。多个地方都有出过问题。唉。
Naughty Stone Piles的更多相关文章
- codeforce 227D Naughty Stone Piles (贪心+递归+递推)
Description There are n piles of stones of sizes a1, a2, -, an lying on the table in front of you. D ...
- Codeforces Round #140 (Div. 2)
A. Where do I Turn? 叉积判断. B. Effective Approach 记录位置. C. Flying Saucer Segments 假设有\(n\)个人,那么\(1\)要移 ...
- upc组队赛17 Stone Game【极小值】
Stone Game 题目链接 题目描述 Alice and Bob are always playing game! The game today is about taking out stone ...
- 2018CCPC桂林站JStone Game
题目描述 Alice and Bob are always playing game! The game today is about taking out stone from the stone ...
- POJ1740A New Stone Game[组合游戏]
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5769 Accepted: 3158 ...
- HDU 4048 Zhuge Liang's Stone Sentinel Maze
Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/327 ...
- POJ 1740 A New Stone Game
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5453 Accepted: 2989 ...
- Light OJ 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- 【POJ】A New Stone Game(博弈论)
http://poj.org/problem?id=1740 题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...
- 【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)
[题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ...
随机推荐
- Zabbix与乐维监控对比分析(一)——架构、性能篇
近年来,Zabbix凭借其近乎无所不能的监控及优越的性能一路高歌猛进,在开源监控领域独占鳌头:而作为后起的新锐IT监控平台--乐维监控,则不断吸收Zabbix,Prometheus等优秀开源平台的优点 ...
- 【每日一题】【字符串与数字互转】【去除空格】【大数处理】2021年12月12日-8. 字符串转换整数 (atoi)
请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数). 函数 myAtoi(string s) 的算法如下: ...
- Spring框架之IOC入门
1.开发环境 IDEA版本: 2022.1.4 Maven版本:3.8.6 Spring版本:6.0.2 2.案例分析 2.1 自定义Man类 2.2 自定义Company类 2.3 自定义Compa ...
- 一文带你搞懂 Google 发布的新开源项目 GUAC
随着软件供应链攻击的显著增加,以及 Log4j 漏洞带来的灾难性后果和影响,软件供应链面临的风险已经成为网络安全生态系统共同关注的最重要话题之一.根据业内权威机构 Sonatype 发布的2022软件 ...
- 手动解析word Table模块内容
最近来了一个需求, 需要手动解析word ( 好处就是不需要安装office 以及不会有office解析的线程残留),然后就是可以自定义解析规则,比较方便 比如解析这个word里面的内容: 标题,表格 ...
- Flutter异常监控 - 贰 | 框架Catcher原理分析
前言 在给 Flutter 应用做异常监控的时候,一开始我是拒绝滴,如果不考虑 Flutter Engine 和 native 侧的监控,用我另一篇文章中不得不知道的 Flutter 异常捕获知识点 ...
- DVWA靶场实战(二)——Command Injection
DVWA靶场实战(二) 二.Command Injection: 1.漏洞介绍: Command Injection,中文叫做命令注入,是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令 ...
- elasticsearch之exists查询
一.exists查询简介 elastic search提供了exists查询,用以返回字段存在值的记录,默认情况下只有字段的值为null或者[]的时候,elasticsearch才会认为字段不存在: ...
- Ubuntu 22.04 BigSur 美化
安装 tweaks sudo apt update && sudo apt upgrade sudo apt install gnome-tweaks gnome-shell-exte ...
- DVWA靶场实战(十一)——XSS(Reflected)
DVWA靶场实战(十一) 十一.XSS(Reflected): 1.漏洞原理: XSS被称为跨站脚本攻击(Cross Site Script),而Reflected被称作反射型XSS.不同于DOM和S ...