A
因为Bob可以换翻一个硬币,除了n=m时的情况,都能弄出有正面的硬币情况
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--) {
int n, m;
cin >> n >> m;
if(n == m) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
C
因为每个点形成的是单向边,所以最后会形成不同的联通块,用并查集维护联通块内权值
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
typedef long long ll;
int a[maxn];
int fa[maxn], tot;
ll ans[maxn], v[maxn];
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
bool cmp(ll a, ll b) {
return a > b;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
fa[i] = i;
scanf("%d", &a[i]);
}
for(int u = 1; u <= n; ++u) {
int v;
scanf("%d", &v);
int fau = find(u), fav = find(v);
if(fau == fav) continue;
fa[min(fau, fav)] = max(fau, fav);
}
for(int i = 1; i <= n; ++i)
v[find(i)] += a[i];
for(int i = 1; i <= n; ++i)
if(v[i]) ans[tot++] = v[i];
sort(ans, ans + tot, cmp);
ll res = 0;
for(int i = 0; i < min(m, tot); ++i) {
res += ans[i];
}
cout << res << endl;
}
E
球冠的体积
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
struct ball{
double x,y,z,r;
ball(double x=0,double y=0,double z=0,double r=0):x(x),y(y),z(z),r(r){}
double dist(ball a){
return sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y)+(z-a.z)*(z-a.z));
}
double v(){
return pi*r*r*r*4/3;
}
}a,b;
double solve(){
double d = a.dist(b);
if(d-a.r-b.r>0)return a.v()+b.v();
double r1=a.r,r2=b.r;
if(d + r1 < r2 || d + r2 < r1) return max(a.v(), b.v());
double v1=pi/3*(r1-(r1*r1-r2*r2+d*d)/(2*d))*(r1-(r1*r1-r2*r2+d*d)/(2*d));
v1 *= 3*r1- (r1-(r1*r1-r2*r2+d*d)/(2*d));
double v2=pi/3*(r2-(r2*r2-r1*r1+d*d)/(2*d))*(r2-(r2*r2-r1*r1+d*d)/(2*d));
v2 *= 3*r2- (r2-(r2*r2-r1*r1+d*d)/(2*d));
return a.v()+b.v()-(v1+v2);
}
int main(){
scanf("%lf%lf%lf%lf",&a.x,&a.y,&a.z,&a.r);
scanf("%lf%lf%lf%lf",&b.x,&b.y,&b.z,&b.r);
printf("%.7f\n",solve());
return 0;
}