JZOJ 1736. 扑克游戏 (Standard IO)
1736. 扑克游戏 (Standard IO)
Time Limits: 1000 ms Memory Limits: 128000 KB
Description
有一棵无穷大的满二叉树,根为star,其余所有点的权值为点到根的距离,如图:
现在你有一些扑克牌,点数从1到13,你要把这些扑克牌全部放到这个树上:
1. 当你把点数为i的扑克牌放在权值为j的点上,那么你会得到i*j的分数。
2. 当你把一个扑克牌放在一个节点上,那么你就不能把别的扑克牌放在这个节点以及这个节点的子树上。
你的目标是最小化你的得分。
Input
文件名为 poker.in
输入第一行为一个数字N,表示你有的扑克牌数;
接下来一行N个数字,数字在1到13之间。
Output
文件名为 poker.out
一个数字,最小得分。
Sample Input
3
5 10 13
Sample Output
43
Data Constraint
Hint
【样例说明】
【数据范围】
30%数据 N<=100
100%数据满足1<=N<=10000.
题解
这是今天做的第四道哈夫曼树专题了
总结一下
解法大同小异,只是换个方式问而已
类似这道题,题目说得分是 i * j 其实就是哈夫曼树,模型转换一下就好了
代码
#include<iostream>
#include<iostream>
#include<cstdio>
#define INF 2147483647
#define N 20000
using namespace std;
long long dui[N*2+1],top;
void add(long x)
{ long now;
dui[++top]=x;
for(now=top;dui[now/2]>dui[now]&&now>1;now/=2)
swap(dui[now],dui[now/2]);
}
long qu()
{ long ans=dui[1],now;
bool t=false;
dui[1]=INF;
now=1;
while(!t){
t=true;
if(now*2==top||dui[now*2]<dui[now*2+1]){
if(dui[now]>dui[now*2]){
swap(dui[now],dui[now*2]);
now=now*2;
t=false;
}
}else if(now*2+1<=top)
if(dui[now]>dui[now*2+1]){
swap(dui[now],dui[now*2+1]);
now=now*2+1;
t=false;
}
}
return ans;
}
int main()
{ long n,m,i,q;
long long ans=0;
scanf("%ld",&n);
for(i=1;i<=n;i++){
scanf("%ld",&q);
add(q);
}
for(i=1;i<n;i++){
q=qu()+qu();
ans+=q;
add(q);
}
printf("%lld\n",ans);
return 0;
}
ps:
看过我其他博文的有没有感觉代码很熟悉
没错,就是合并果子
JZOJ 1736. 扑克游戏 (Standard IO)的更多相关文章
- JZOJ 1349. 最大公约数 (Standard IO)
1349. 最大公约数 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 小菜的妹妹小诗就要读小学了!正所谓 ...
- JZOJ 1776. 经济编码 (Standard IO)
1776. 经济编码 (Standard IO) Time Limits: 1000 ms Memory Limits: 128000 KB Description 为降低资料储存的空间或增加资料传送 ...
- JZOJ 1774. 合并果子 (Standard IO)
1774. 合并果子 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 在一个果园里,多多已经将所有的果子打 ...
- 1003. 猜数游戏 (Standard IO)
题目描述 有一个“就是它”的猜数游戏,步骤如下:请你对任意输入的一个三位数x,在这三位数后重复一遍,得到一个六位数,467-->467467.把这个数连续除以7.11.13,输出最后的商. 输入 ...
- JZOJ 2137. 【GDKOI2004】城市统计 (Standard IO)
2137. [GDKOI2004]城市统计 (Standard IO) Time Limits: 1000 ms Memory Limits: 128000 KB Detailed Limits ...
- JZOJ 5326. LCA 的统计 (Standard IO)
5326. LCA 的统计 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description Input Output S ...
- JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)
5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...
- JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林 (Standard IO)
5286. [NOIP2017提高A组模拟8.16]花花的森林 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Descript ...
- JZOJ 5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)
5305. [NOIP2017提高A组模拟8.18]C (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description ...
随机推荐
- AdminSwagger2Configuration
package org.linlinjava.litemall.admin.config; import org.springframework.context.annotation.Bean; im ...
- 源码分析SpringBoot启动
遇到一个问题,需要从yml文件中读取数据初始化到static的类中.搜索需要实现ApplicationRunner,并在其实现类中把值读出来再set进去.于是乎就想探究一下SpringBoot启动中都 ...
- CountingSort(计数排序)原理及C++代码实现
计数排序是需要假设输入数据的排序之一,它假设输入元素是0到k区间内的一个整数,其中k为某个整数.当k=O(n)时,计数排序的时间复杂度为θ(n). 因为不是通过比较来排序,所以它的时间复杂度可以达到θ ...
- jQ给下拉框绑定事件,为什么要绑定在框(select标签)上,而不是绑定在选项(option标签)上
这是我在学习锋利的 jquery 书中 5.1.4 的代码时遇到的一个小问题,源代码如下: <head> <style type="text/css"> * ...
- spring boot 配置文件properties和YAML详解
spring boot 配置文件properties和YAML详解 properties中配置信息并获取值. 1:在application.properties配置文件中添加: 根据提示创建直接创建. ...
- flink测试用例编写
使用tableFunction的collect总是npe, 实际可以自定义collector, 在collector中做自己想做的事 不使用现成的collector
- [LC] 116. Populating Next Right Pointers in Each Node
You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...
- [LC] 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 同步linux系统时间
Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...
- 跨越真实和虚拟世界的边界——走近SIGGRAPH 2014大会
2014大会" title="跨越真实和虚拟世界的边界--走近SIGGRAPH 2014大会"> 作者:孙鑫 微软亚洲研究院研究员 一场大会振奋一座城 温哥华位于加 ...