AtCoder Beginner Contest 331

C - Sum of Numbers Greater Than Me

题意:给定一个长度为n的数组,分别找出比a[i]大的数组里的数的和

思路:用map记录每个数的个数,然后遍历一遍,后面一个项就是数组之和-前面一项-前面的累加

#define int long long
using namespace std;
const int N=2e6+10;
int ans[N],a[N];
void solve(){
	int n;
	cin>>n;
	map<int,int>mp;
	int sum=0;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		a[i]=x;
		mp[x]++;
		sum+=x;
	}
	int t=0;
	for(auto c:mp){
		int x=c.first;
		ans[x]=sum-(c.first*c.second)-t;
		t+=c.first*c.second;
	}
	for(int i=1;i<=n;i++){
		cout<<ans[a[i]]<<" ";
	}
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	//cin>>t;
	for(int i=1;i<=t;i++)solve();
	return 0;
} 

本文作者:yufan1102

本文链接:https://www.cnblogs.com/yufan1102/p/17889549.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   yufan1102  阅读(41)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.