跳到正文

个人记录

123

__Groyhj__QwQ约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月4日
篇幅
约 2 分钟

版本

  1. v1当前

dgl的礼物 题解

本人认为这道题还是挺简单的。

1. 思路分析

首先,我们很容易想到定义两个数组 aa、bb,分别存储每个人送出的礼物和收到的礼物。

然后,对于每一条送礼关系(xx 同学给 yy 同学送礼),xx 同学送出的礼物数加一,yy 同学收到的礼物数加一,即 ax←ax+1,by←by+1a_x \gets a_x+1 , b_y \gets b_y+1。

最后,设一共要准备的礼物数为 ansans,对于第 ii 位同学,他要准备的礼物数为送出的礼物数减去收到的礼物数,如果为负数,就代表收到的礼物数比送出的礼物数多,那么他就不用准备礼物,ansans 就是每个同学要准备的礼物数的总和。即

ans=∑i=1nmax⁡(bi−ai,0)ans=\sum^{n}_{i=1} \max(b_i-a_i,0)

2. AC 代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[402],b[402];
signed main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y;
		cin>>x>>y;
		a[x]++;
		b[y]++;
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		ans+=max(b[i]-a[i],0LL);
	}
	cout<<ans<<endl;
	return 0;
}
原文