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, ...
随机推荐
- 一文带你快速入门 Go 语言微服务开发 - Dubbo Go 入门实践总结
更多详细示例可直接访问 Dubbo 官网 或搜索关注官方微信公众号:Apache Dubbo 1. 安装Go语言环境 建议使用最新版 go 1.17 go version >= go 1.15 ...
- 1.2 Hadoop简介-hadoop-最全最完整的保姆级的java大数据学习资料
目录 1.2 Hadoop简介 1.2.1 什么是Hadoop 1.2.2 Hadoop的起源 1.2.3 Hadoop的特点 1.2.4 Hadoop的发行版本 1.2.5 Apache Hadoo ...
- GitHub上的一个笔记相关小项目
就是一个笔记屑小项目, C++编写,有想一起开发的私信 AlgorithWeaver/V-note (github.com) 项目名V-note QVQ
- Linux相关命令及软件安装教程
@font-face { font-family: "Times New Roman" } @font-face { font-family: "宋体" } @ ...
- K8S 核心组件 kubelet 与 kube-proxy 分析
kubelet kubelet 进程用于处理master 下发的任务, 管理pod 中的容器, 注册 自身所在的节点. 节点管理 启动参数说明 --register-node #如果设置为true 则 ...
- Python全栈工程师之从网页搭建入门到Flask全栈项目实战(4) - Flask模板语法与继承
1.Flask模板介绍 前置:理解渲染机制即上篇笔记中render_template()功能是如何实现的! 1)找到html文件地址 2)读取html文件中的内容 3)替换html中的特殊字符 4)将 ...
- MassTransit 知多少 | 基于MassTransit Courier实现Saga 编排式分布式事务
Saga 模式 Saga 最初出现在1987年Hector Garcaa-Molrna & Kenneth Salem发表的一篇名为<Sagas>的论文里.其核心思想是将长事务拆分 ...
- List排序(降序)
一.添加一个比较器 点击查看代码 import java.util.Comparator; /** * @Classname ComparatorResultType * @Description 排 ...
- python 之选择结构(if --elif --else)
python中有三种结构:顺序结构.选择结构.循环结构,此处介绍选择结构. if -- else 结构: if 判断条件: 执行语句 else: 执行语句 当if后面的判断条件为真(True)时,执行 ...
- GeoLayout: Geometry Driven Room Layout Estimation Based on Depth Maps of Planes
1. 论文简介 论文题目:GeoLayout: Geometry Driven Room Layout Estimation Based on Depth Maps of Planes Paper地址 ...