题解:P11393 [JOI Open 2019] 汇款 / Remittance
对于这个题,一个比较关键的观察是:从任意一点流出的流量都只会流
然后我们考虑一下其他的东西:
若对于
若
不过若
然后直接硬模拟就行了,最多会流
:::success[AC code]
#include<bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 1e6+1;
int n,a[N],b[N];
inline int calc(int x){
int cnt = 0;
while(x){
++cnt;
x>>=1;
}
return cnt;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin >> n;
int maxn = 0;
for(int i = 1;i<=n;++i){
cin >> a[i] >> b[i];
maxn = max(maxn,a[i]);
}
int tot = calc(maxn);
tot+=n;
int flow = 0;
for(int cnt = 1,i = 1;cnt<=tot;++cnt){
flow>>=1;
a[i]+=flow;
flow = 0;
if(a[i]>b[i]){
if((a[i]-b[i])&1){
if(b[i]){
flow+=a[i]-(b[i]-1);
a[i] = b[i]-1;
}else{
flow+=a[i]-(b[i]+1);
a[i] = b[i]+1;
}
}else{
flow+=a[i]-b[i];
a[i] = b[i];
}
}
++i;
if(i == n+1){
i = 1;
}
}
for(int i = 1;i<=n;++i){
if(a[i]!=b[i]){
cout << "No";
return 0;
}
}
cout << "Yes";
return 0;
}
:::