题目: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的更多相关文章

  1. 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 ...

  2. Codeforces Round #140 (Div. 2)

    A. Where do I Turn? 叉积判断. B. Effective Approach 记录位置. C. Flying Saucer Segments 假设有\(n\)个人,那么\(1\)要移 ...

  3. upc组队赛17 Stone Game【极小值】

    Stone Game 题目链接 题目描述 Alice and Bob are always playing game! The game today is about taking out stone ...

  4. 2018CCPC桂林站JStone Game

    题目描述 Alice and Bob are always playing game! The game today is about taking out stone from the stone ...

  5. POJ1740A New Stone Game[组合游戏]

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5769   Accepted: 3158 ...

  6. 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 ...

  7. POJ 1740 A New Stone Game

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5453   Accepted: 2989 ...

  8. Light OJ 1296 - Again Stone Game (博弈sg函数递推)

    F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. 【POJ】A New Stone Game(博弈论)

    http://poj.org/problem?id=1740 题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...

  10. 【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)

    [题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ...

随机推荐

  1. 谈谈我的「数字文具盒」 - NextCloud

    接下来两篇主要谈论 Nextcloud 和 Obsidian,因为篇幅较长,所以单出罗列出来.本文主要介绍 Nextcloud 以及使用中的技巧和心得体会. Nextcloud Nextcloud 是 ...

  2. 【SQL真题】SQL2:平均播放进度大于60%的视频类别

    题目:https://www.nowcoder.com/practice/c60242566ad94bc29959de0cdc6d95ef?tpId=268&tqId=2285039& ...

  3. 使用 SmartIDE 开发golang项目

    目录 概述 架构 开发视图 快速开始 安装 SmartIDE CLI 环境 启动 创建环境 安装工具 调试 基本调试 Start 命令调试 很荣幸在去年加入到 SmartIDE 产品组,从事开发工作, ...

  4. SQLMap入门——获取表中的字段名

    查询表名之后,查询表中的字段名 python sqlmap.py -u http://localhost/sqli-labs-master/Less-1/?id=1 -D xssplatform -T ...

  5. 浅聊一下Django如何避免xss攻击

    一.什么是xss攻击 xss攻击:----->web注入 xss跨站脚本攻击(Cross site script,简称xss)是一种"HTML注入",由于攻击的脚本多数时候是 ...

  6. 实践GoF的23种设计模式:命令模式

    摘要:命令模式可将请求转换为一个包含与请求相关的所有信息的对象, 它能将请求参数化.延迟执行.实现 Undo / Redo 操作等. 本文分享自华为云社区<[Go实现]实践GoF的23种设计模式 ...

  7. 记一次在CentOS上安装GitLab的流程

    1.本次环境说明 系统:Centos7.6 IP地址:http://192.168.3.213: 最低配置要求:2核心CPU和4G内存,这是因为[GitLab]的整体运行包含了多个进程  2.自行安装 ...

  8. 学习ASP.NET Core Blazor编程系列十九——文件上传(下)

    学习ASP.NET Core Blazor编程系列文章之目录 学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应 ...

  9. SQL Server下7种“数据分页”方案,全网最全

    数据分页往往有三种常用方案. 第一种,把数据库中存放的相关数据,全部读入PHP/Java/C#代码/内存,再由代码对其进行分页操作(速度慢,简易性高). 第二种,直接在数据库中对相关数据进行分页操作, ...

  10. Python Kconfiglib初次学习

    1 参考 kconfiglib库官方介绍:kconfiglib · PyPI Kconfiglib源码:GitHub - ulfalizer/Kconfiglib: A flexible Python ...