Gym 102920
B - Commemorative Dice
答案:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
int a[101],b[101];
int main()
{
for(int i=1;i<=6;i++)
{
cin>>a[i];
}
for(int i=1;i<=6;i++)
{
cin>>b[i];
}
int ans=0;
for(int i=1;i<=6;i++)
{
for(int j=1;j<=6;j++)
{
if(a[i]>b[j])
ans++;
}
}
int k=__gcd(ans,36);
cout<<ans/k<<'/'<<36/k<<endl;
}
C - Dessert Café
答案:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>h[1010000];
int vis[1001000];
bool bk[1001000];
int n,m;
int ans=0;
void dfs(int u,int x)
{
int cnt=0;
for(int i=0; i<h[u].size(); i++)
{
if(h[u][i]!=x)
{
dfs(h[u][i],u);
vis[u]+=vis[h[u][i]];
if(vis[h[u][i]])
cnt++;
}
}
if(vis[u]!=m)
cnt++;
if(cnt>=2&&!bk[u])
ans++;
}
int main()
{
cin>>n>>m;
for(int i=1; i<n; i++)
{
int u,v,w;
cin>>u>>v>>w;
h[u].push_back(v);
h[v].push_back(u);
}
int x;
for(int i=0; i<m; i++)
{
cin>>x;
vis[x]=1;
bk[x]=true;
}
ans=m;
dfs(1,-1);
cout<<ans<<endl;
return 0;
}
E - Imprecise Computer
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int MOD = 1e4+7;
const int N = 2e6 + 10;
const int M = 1111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
int dxy[][2]={{0,1},{1,0},{1,1},{-1,1}};
using namespace std;
int dp[N];
void solve(){
int n;
cin>>n;
rep(i,1,n) cin>>dp[i];
rep(i,1,n-1){
if(dp[i]>1){
puts("NO");
return ;
}
if(dp[i]){
if(dp[i+1]) dp[i+1]--;
else dp[i+1]++;
}
}
if(!dp[n]) puts("YES");
else puts("NO");
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
G - Mobile Robot
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int MOD = 1e4+7;
const int N = 2e6 + 10;
const int M = 1111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
int dxy[][2]={{0,1},{1,0},{1,1},{-1,1}};
using namespace std;
ll dp[N];
void solve(){
ll n;
ll key;
cin>>n>>key;
ll maxn1=-inf;
ll minn1=inf;
ll maxn2=-inf;
ll minn2=inf;
rep(i,1,n){
cin>>dp[i];
maxn1=max(maxn1,-(i-1)*key+dp[i]);
minn1=min(minn1,-(i-1)*key+dp[i]);
maxn2=max(maxn2,(i-1)*key+dp[i]);
minn2=min(minn2,(i-1)*key+dp[i]);
}
ll res1=maxn1-minn1;
ll res2=maxn2-minn2;
ll ans=min(res1,res2);
if(ans%2==0) cout<<ans/2<<'.'<<0<<endl;
else cout<<ans/2<<'.'<<5<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
L - Two Buildings (补)
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int MOD = 1e4+7;
const int N = 2e6 + 10;
const int M = 1111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
int dxy[][2]={{0,1},{1,0},{1,1},{-1,1}};
using namespace std;
struct node{
ll x;
ll y;
};
node a[N],b[N];
ll vis[N];
ll dp[N];
ll n,m;
bool cmp(node x,node y){
if(x.x==y.x) return x.y<y.y;
return x.x<y.x;
}
ll cal(ll j,ll i){
return (a[j].y-b[i].y)*(a[j].x-b[i].x);
}
ll judge(ll l1,ll r1,ll l2,ll r2){
if(l1>r1||l2>r2) return -inf;
ll mid=(l1+r1)>>1;
ll pos=l2;
ll maxn=cal(mid,pos);
rep(i,l2+1,r2){
ll t=cal(mid,i);
if(t>=maxn){
maxn=t;
pos=i;
}
}
return max(maxn,max(judge(l1,mid-1,l2,pos),judge(mid+1,r1,pos,r2)));
}
void solve(){
cin>>n;
m=n;
rep(i,1,n){
cin>>dp[i];
a[i]={i,dp[i]};
b[i]={i,-dp[i]};
}
sort(a+1,a+1+n,cmp);
sort(b+1,b+1+n,cmp);
rep(i,1,n) vis[i]=0;
ll last=n;
ll cnt=0;
bep(i,n-1,1){
if(a[i].y<=a[last].y) vis[i]=1;
else last=i;
}
rep(i,1,n){
if(!vis[i]) a[++cnt]=a[i];
}
n=cnt;
rep(i,1,m) vis[i]=0;
last=1;
cnt=0;
rep(i,2,m){
if(b[i].y>=b[last].y) vis[i]=1;
else last=i;
}
rep(i,1,m){
if(!vis[i]) b[++cnt]=b[i];
}
m=cnt;
ll res=judge(1,n,1,m);
cout<<res<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
H - Needle(补 FFT)
答案:
#include <bits/stdc++.h>
using namespace std;
const int maxm=2e6+5;
const double P=acos(-1.0);
struct CC{//复数
double x,y;
CC(double xx=0,double yy=0){x=xx,y=yy;}
CC operator+(const CC &a)const{return CC(x+a.x,y+a.y);}
CC operator-(const CC &a)const{return CC(x-a.x,y-a.y);}
CC operator*(const CC &a)const{return CC(x*a.x-y*a.y,x*a.y+y*a.x);}
};
int a[maxm];
int b[maxm];
int c[maxm];
int n,m,p;
CC x1[maxm],x2[maxm];
int sum[maxm];
int rev[maxm];
void fft(CC y[],int len,int on){//on为1或者-1,-1的时候表示逆变换
for(int i=0;i<len;i++)if(i<rev[i])swap(y[i],y[rev[i]]);
for(int h=2;h<=len;h<<=1){
CC wn(cos(-on*2*P/h),sin(-on*2*P/h));
for(int j=0;j<len;j+=h){
CC w(1,0);
for(int k=j;k<j+h/2;k++){
CC u=y[k];
CC t=w*y[k+h/2];
y[k]=u+t;
y[k+h/2]=u-t;
w=w*wn;
}
}
}
if(on==-1){
for(int i=0;i<len;i++){
y[i].x/=len;
}
}
}
signed main(){
int maa=0,mab=0,mac=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
a[i]+=3e4;
maa=max(maa,a[i]);
}
cin>>m;
for(int i=1;i<=m;i++){
cin>>b[i];
b[i]+=3e4;
mab=max(mab,b[i]);
}
cin>>p;
for(int i=1;i<=p;i++){
cin>>c[i];
c[i]+=3e4;
mac=max(mac,c[i]);
}
int len=1,l=0;
while(len<=maa+mac)len<<=1,l++;
for(int i=0;i<len;i++){
rev[i]=(rev[i>>1]>>1)|((i&1)<<(l-1));
}
for(int i=1;i<=n;i++){
x1[a[i]].x+=1;
}
for(int i=1;i<=p;i++){
x2[c[i]].x+=1;
}
fft(x1,len,1);
fft(x2,len,1);
for(int i=0;i<len;i++){
x1[i]=x1[i]*x2[i];
}
fft(x1,len,-1);
for(int i=0;i<=len;i++){
sum[i]=(int)(x1[i].x+0.5);
}
len=n+m;
int ans=0;
for(int i=1;i<=m;i++){
ans+=sum[b[i]*2];
}
cout<<ans<<endl;
return 0;
}