11 .Codeforces Round 891 (Div. 3)E. Power of Points(推公式+前缀和优化)
E. Power of Points
题解参考
#include <bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define fep(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i=(a); i<(b); ++i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define x first
#define y second
#define pb push_back
#define vi vector<int>
using namespace std;
const int maxn = 1e5 + 10;
//int l[maxn],r[maxn],n;
struct node {
int val, pos;
bool operator<(const node &t) const {
return val < t.val;
}
};
void solve() {
int n;
cin >> n;
vector<node> a(n + 1);
vi l(n + 1), r(n + 2);
rep(i, 1, n) {
int k;
cin >> k;
a[i] = {k, i};
}
sort(a.begin() + 1, a.end());
//预处理前缀和、后缀和
rep(i, 1, n) {
l[i] = l[i - 1] + a[i].val;
}
r[n] = a[n].val;
fep(i, n - 1, 1) {
r[i] = r[i + 1] + a[i].val;
}
vi ans(n + 1);
rep(i, 1, n) {
int res = n + (2 * i - n) * a[i].val - l[i] + r[i + 1];
ans[a[i].pos] = res;
}
rep(i, 1, n) {
cout << ans[i] << ' ';
}
cout << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("C:\\Users\\24283\\CLionProjects\\untitled2\\1.in", "r", stdin);
int _;
cin >> _;
while (_--)
solve();
return 0;
}
总结
- 注意下标
11 .Codeforces Round 891 (Div. 3)E. Power of Points(推公式+前缀和优化)的更多相关文章
- Codeforces Round #133 (Div. 2), A.【据图推公式】 B.【思维+简单dfs】
Problem - 216A - Codeforces Problem - B - Codeforces A Tiling with Hexagons 题意: 给出a b c ,求里面有多少个六边形 ...
- Codeforces Round #596 (Div. 2)D.Power Products
题意: 给一个数组,给你一个k,找出两个数字的积可以变成xk的数对对数 解析: 当且仅当,两个数进行质因子分解后每个因子的个数都是k的倍数个就说明这是满足条件的一对,可以让每个因子个数%k用map找对 ...
- [每日一题2020.06.11]Codeforces Round #644 (Div. 3) H
A-E见 : 这里 题目 我觉得很有必要把H拿出来单独发( 其实是今天懒得写题了 ) problem H 一个从 1 到 $ 2^m - 1$ 的长度为m的连续二进制序列, 删去指定的n个数, 问剩余 ...
- Codeforces Round #666 (Div. 2) B. Power Sequence (枚举)
题意:有一个长度为\(n\)的序列,你每次可以对序列重新排序,然后花费\(1\)使某个元素加减\(1\),多次操作后使得新序列满足\(a_{i}=c^i\),\(c\)是某个正整数,求最小花费. 题解 ...
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #331 (Div. 2)C. Wilbur and Points 贪心
C. Wilbur and Points Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/ ...
- Codeforces Round #248 (Div. 1) B. Nanami's Digital Board 暴力 前缀和
B. Nanami's Digital Board 题目连接: http://www.codeforces.com/contest/434/problem/B Description Nanami i ...
- Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)
http://codeforces.com/contest/816/problem/B To stay woke and attentive during classes, Karen needs s ...
- Codeforces Round #331 (Div. 2) C. Wilbur and Points
C. Wilbur and Points time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun 四维前缀和
D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...
随机推荐
- Unity SetActive Event
网上查了一下Unity的SetActive变化事件没有找到,我想到用另一种思路来实现这个事件通知,它可用来调试是何处把某个gameobject隐藏掉了 Unity提供了这两个函数,OnEnable,O ...
- gRPC学习小札
gRPC 前言 为什么使用gRPC 传输协议 传输效率 性能消耗 gRPC入门 gRPC流 证书认证 使用根证书 gRPC实现token认证 和Web服务共存 验证器 REST接口 grpcurl工具 ...
- Windows上部署Python flask项目
最近使用Python flask做了一个项目要部署,网上一大堆教程没有一个完整,最后看了多个教程才配置完成,下面根据自己的环境整理一下做个备忘录 环境: Windows 10 apache httpd ...
- 内存泄漏定位工具之 mtrace(一)
1 前言 mtrace(memory trace),是 GNU Glibc 自带的内存问题检测工具,它可以用来协助定位内存泄露问题.它的实现源码在glibc源码的malloc目录下,其基本设计原理为设 ...
- yapi的安装
1.官方网站:https://hellosean1025.github.io 2.看官方的文档,部署方法: 3. 根据文档 安装环境先: 4. 开始安装yapi 5. 可见需要启动一下 6.重启一下 ...
- [JVM]GC日志解读解析
GC日志解读解析 示例代码 package jvm.test1; import java.util.Random; import java.util.concurrent.TimeUnit; impo ...
- JS Leetcode 503. 下一个更大元素 II 题解分析,依旧单调栈做法解决此题
壹 ❀ 引 我在JS Leetcode 496. 下一个更大元素 I 更清晰的图解单调栈做法一文中,介绍了单调栈做法解决下一个更大元素的问题,比较巧的是这道题还有升级版,题目来自Leetcode503 ...
- JS leetcode 移除元素 题解分析
壹 ❀ 引 又到了每日一道算法题的环节,今天做的题同样非常简单,题目来源leetcode27. 移除元素,题目描述如下: 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 va ...
- 源码剖析Spring依赖注入:今天你还不会,你就输了
在之前的讲解中,我乐意将源码拿出来并粘贴在文章中,让大家看一下.然而,我最近意识到这样做不仅会占用很多篇幅,而且实际作用很小,因为大部分人不会花太多时间去阅读源码. 因此,从今天开始,我将采取以下几个 ...
- OAuth2 Authorization Server
基于Spring Security 5 的 Authorization Server的写法 先看演示 pom.xml <?xml version="1.0" encoding ...