ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2020.9.25

2020-09-25 13:00:33  阅读:219  来源: 互联网

标签:25 ch 2020.9 int long while include getchar


SMZX的NOIP模拟赛7

这场状态不好,只过了一道。(结果又是赛后灵光乍现,想了想就知道后两题怎么做了......)

T1

赛后翻到了原题,CF1311F Moving Points

赛时唯一过的一道,蛮简单的。

对于任意两个点,当靠左的点能追上右边的点时,它们一定能重合,因为时间可以不是整数。

分类讨论所有可能的情况后可归纳出:

左边的点速度比右边的时,它们一定能重合;否则两个点只会越跑距离越远,能贡献给答案的便是初始位置的距离。

所以排个序上个树状数组就好了。

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
int read() {
	char ch = getchar();
	int x = 0, pd = 0;
	while (ch < '0' || ch > '9')
		pd ^= ch == '-', ch = getchar();
	while ('0' <= ch && ch <= '9')
		x = x*10+(ch^48), ch = getchar();
	return pd ? -x : x;
}
const int maxn = 200005;
int n;
struct Node {
	int p, v;
}nd[maxn];
int b[maxn], N;
bool cmp(Node x, Node y) {
	return x.p < y.p;
}
LL s[maxn];
int cnt[maxn];
void add(int x, int y) {
	while (x <= N)
		s[x] += y, ++cnt[x], x += x&-x;
}
LL queryS(int x) {
	LL res = 0;
	while (x) res += s[x], x -= x&-x;
	return res;
}
int queryCnt(int x) {
	int res = 0;
	while (x) res += cnt[x], x -= x&-x;
	return res;
}
LL ans;
int main() {
	n = read();
	for (int i = 1; i <= n; i++)
		nd[i].p = read();
	for (int i = 1; i <= n; i++)
		b[i] = nd[i].v = read();
	sort(b + 1, b + n + 1), N = unique(b + 1, b + n + 1) - b - 1;
	sort(nd + 1, nd + n + 1, cmp);
	int pos;
	pos = lower_bound(b + 1, b + N + 1, nd[n].v) - b;
	add(pos, nd[n].p);
	LL totS = nd[n].p;
	for (int i = n - 1; i; i--) {
		pos = lower_bound(b + 1, b + N + 1, nd[i].v) - b;
		ans += (totS - queryS(pos - 1)) - 1ll * (n - i - queryCnt(pos - 1)) * nd[i].p;
		totS += nd[i].p;
		add(pos, nd[i].p);
	}
	printf("%lld\n", ans);
	return 0;
}

标签:25,ch,2020.9,int,long,while,include,getchar
来源: https://www.cnblogs.com/smsylby/p/13729506.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有