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, ...
随机推荐
- ssm 乱码
1.post乱码:在web.xml中增加解决解决post乱码的过滤器 <!--解决POST乱码问题--> <filter> <filter-name>Charact ...
- AI绘画提示词创作指南:DALL·E 2、Midjourney和 Stable Diffusion最全大比拼 ⛵
作者:韩信子@ShowMeAI 深度学习实战系列:https://www.showmeai.tech/tutorials/42 自然语言处理实战系列:https://www.showmeai.tech ...
- HashMap为何线程不安全?HashMap,HashTable,ConcurrentHashMap对比
这两天写爬虫帮组里收集网上数据做训练,需要进一步对收集到的json数据做数据清洗,结果就用到了多线程下的哈希表数据结构,猛地回想起自己看<Java并发编程的艺术>框架篇的时候,在Concu ...
- JavaEE Day07 HTML
今日内容 Web概念概述 HTML 一.Web概念概述 1. JavaWeb:使用Java语言开发的基于互联网的项目 2.软件架构 C/S架构:Client/Server--- 客户端/服务器端(安卓 ...
- 异构混排在vivo互联网的技术实践
作者:vivo 互联网算法团队- Shen Jiyi 本文根据沈技毅老师在"2022 vivo开发者大会"现场演讲内容整理而成. 混排层负责将多个异构队列的结果如广告.游戏.自然量 ...
- django中如何开启事务
一:django中如何开启事务 1.事务的四大特征 ACID A: 原子性 每个事务都是不可分割的最小单位(同一个事物内的多个操作要么同时成功要么同时失败) C: 一致性 事物必须是使数据库从一个一致 ...
- vue 强制刷新数据 this.$forceUpdate()
vue项目中,修改了数据可能已经渲染的地方不会发生变化,所以加上 this.$forceUpdate()可以强制刷新数据
- uniapp微信小程序 下拉刷新(上拉请求下一页数据)
<template>标签中: <view class="" v-if="daShow==1"> <view class=" ...
- overflow:scroll修改样式
当overflow :scroll 出现滚动条后,默认的滚动条样式太丑了,不是我们想要的,那么我们来修改一下吧!~ 话不多说,直接上代码 /* 定义滚动条样式 */ ::-webkit-scroll ...
- 10、RestTemplate方式实现远程调用Client
一.JSONObject类详解: JSONobject是FastJson提供的对象,在API中是用一个私有的常量map进行封装的,实际就是一个map,只不过 FastJson对其进行了封装,添加了很多 ...