CF1492B Card Deck 题解
Content
有 \(n\) 张纸牌组成的一个牌堆,每张纸牌都有一个价值 \(p_1,p_2,\dots,p_n\)。每次选出最顶上的几个牌放到另外一个一开始为空的牌堆里面。定义一个牌堆的总值为 \(\sum\limits_{i=1}^nn^{n-i} \cdot p_i\)。请构造出一个可能的最终的牌堆,使得这个牌堆的总值最大。
数据范围:\(t\) 组数据,\(t\in[1,10^3]\),\(1\leqslant p_i\leqslant n\leqslant 10^5\)。
Solution
我们不难想到,需要尽可能将 \(p_i\) 大的牌放到前面去,因此我们想到这样一个贪心算法:将前 \(1\sim n\) 个数的最大值所在位置记为 \(maxi_i\)(\(maxi_i\) 可以通过边读入边记录,详见代码),然后从牌顶开始,每次将 \(maxi_i\) 相等的牌按照原来的顺序放入新的牌堆,这样可以保证将 \(p_i\) 大的牌放在前面而又符合要求。
Code
int n, p[100007], maxi[100007], ans[100007], cnt;
int main() {
MT {
memset(ans, 0, sizeof(ans));
memset(maxi, 0, sizeof(maxi));
n = Rint, cnt = 0;
F(i, 1, n) {
p[i] = Rint;
if(p[i] > p[maxi[i - 1]]) maxi[i] = i, ans[++cnt] = i;
else maxi[i] = maxi[i - 1];
}
ans[++cnt] = n + 1;
R(i, cnt, 1) F(j, ans[i], ans[i + 1] - 1) printf("%d ", p[j]);
puts("");
}
return 0;
}
CF1492B Card Deck 题解的更多相关文章
- A Pythonic Card Deck: __len__ & __getitem__ & for 循环的嵌套
1. 列表生成式的嵌套 for 循环: 示例如下: li1 = range(1,6) li2 = list("ABC") # list("ABC") 的结果为 ...
- Clash Credenz 2014 Wild Card Round题解
A题 简单模拟. /************************************************************************* > File Name: ...
- CF106A Card Game 题解
Content 一种纸牌游戏有许多张卡牌,每张卡牌有一个套装(S,H,D和 C)和一个等级(按升序排列6,7,8,9,T, J,Q,K,A). 规定以下情况卡牌 A 才能赢卡牌 B: A 有王牌套装, ...
- 浙大pat1042题解
1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shu ...
- [LeetCode] 914. X of a Kind in a Deck of Cards 一副牌中的X
In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...
- Card Stacking 队列模拟
题目链接:https://ac.nowcoder.com/acm/contest/993/ABessie is playing a card game with her N-1 (2 <= N ...
- Codeforces Round#704 Div2 题解(A,B,C,D,E)
FST ROUND !!1 A Three swimmers: 直接整除一下向上取整就好了: #include <bits/stdc++.h> using namespace std; t ...
- HDU 5816 Hearthstone (状压DP)
Hearthstone 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5816 Description Hearthstone is an onlin ...
- CodeForces 546C(队列)
CodeForces 546C Soldier and Cards Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I ...
随机推荐
- 洛谷 P6295 - 有标号 DAG 计数(生成函数+容斥+NTT)
洛谷题面传送门 看到图计数的题就条件反射地认为是不可做题并点开了题解--实际上这题以我现在的水平还是有可能能独立解决的( 首先连通这个条件有点棘手,我们尝试把它去掉.考虑这题的套路,我们设 \(f_n ...
- Codeforces 700D - Huffman Coding on Segment(莫队+根分)
Codeforces 题目传送门 & 洛谷题目传送门 好家伙,刚拿到此题时我连啥是 huffman 编码都不知道 一种对 \(k\) 个字符进行的 huffman 编码的方案可以看作一个由 \ ...
- Codeforces Round #732 (Div.1) 题解
实在是打击人信心的一场比赛啊--一不注意就掉了 50+ 分,rating 没了啊/ll/dk/wq/kk A Weak pretest!!!!!11 /fn/fn/fn 一个很显然的注意点是在交换前后 ...
- GWAS与GS模型介绍与比较
目录 1.GWAS模型 1.1卡方检验 1.2 相关性系数的t检验 1.3 一般线性模型GLM 1.4 混合线性模型MLM 1.5 压缩混合线性模型CMLM 1.6 SUPER 1.7 FarmCPU ...
- Docker实用命令介绍
Docker实用命令介绍 1. docker启动.关闭.停止 ╭─wil-xz in ~ 12:15:44 ╰─٩(ŏ﹏ŏ.)۶ service docker restart Redirecting ...
- C语言 指针数组指针
指向指针数组的指针. 1 #include <stdio.h> 2 3 int main(void) 4 { 5 char *pa[4]={"aaaaa"," ...
- MybatisPlus使用Wrapper实现查询功能
Wrapper---条件查询器 :使用它可以实现很多复杂的查询 几个案例 环境: 参照博客:MybatisPlus入门程序 1.条件查询 1.1 查询name不为空的用户,并且邮箱不为空的用户,年龄大 ...
- C#表格,表格信息、GridView使用。
page: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="test1.a ...
- 商业爬虫学习笔记day1
day1 一. HTTP 1.介绍: https://www.cnblogs.com/vamei/archive/2013/05/11/3069788.html http://blog.csdn.ne ...
- 01 nodejs MVC gulp 项目搭建
文本内容 使用generator-express创建nodejs MVC DEMO 使用gulp实时编译项目 npm安装二进制包,无须再编译wget https://nodejs.org/dist/v ...