ACM-NEFU15屆校賽-大二組
阿新 • • 發佈:2020-12-15
A.小林找工作
#include<bits/stdc++.h> using namespace std; const int MAXN=1e5+10; int p[MAXN]; int main() { int n,m; cin>>n>>m; for(int i=1;i<=n;i++) { scanf("%d", &p[i]); } sort(p+1,p+n+1); for(int i=0;i<m;i++) { int temp; scanf("%d", &temp); int pos=lower_bound(p+1,p+n+1,temp)-p; if(pos>n) pos=n; int ans=abs(temp-p[pos]); if(pos!=1) ans=min(ans, abs(temp-p[pos-1])); printf("%d\n", ans); } return 0; }
B.xx的樹
#include<bits/stdc++.h> using namespace std; const int MAXN=2e5+10; typedef long long ll; typedef struct{ int to; int next; }edge; edge e[MAXN]; int cur=1,head[MAXN],vis[MAXN]; ll value[MAXN]; void add(int u,int v) { e[cur].to=v; e[cur].next=head[u]; head[u]=cur++; } void dfs(int u) { vis[u]=1; for(int i=head[u];i!=0;i=e[i].next) { int v=e[i].to; if(vis[v]) continue; value[v]+=value[u]; dfs(v); } } int main() { int n,m; cin>>n>>m; for(int i=1;i<n;i++) { int u,v; cin>>u>>v; add(u,v); add(v,u); } for(int i=1;i<=m;i++) { ll u,d; cin>>u>>d; value[u]+=d; } dfs(1); for(int i=1;i<=n;i++) i==1?cout<<value[i]:cout<<" "<<value[i]; return 0; }
C.xx玩遊戲
找到規律,偶數列沒有必敗態,奇數列有(n+1)/2個必敗態
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin>>t;
while(t--)
{
ll n;
scanf("%lld", &n);
ll t=(n+1)/2;
ll lose=(1+t)*t/2;
ll sum=(1+n)*n/2;
printf("%lld\n", sum-lose);
}
return 0;
}
D. xx的零食店(待補)
E.qyh的簽到題
跟去年校賽大一的一道題神似...
#include<bits/stdc++.h>
using namespace std;
int n,q;
typedef long long ll;
const long long mod=1e9+7;
const int MAXN=1e5+5;
ll d1[MAXN],d2[MAXN],d3[MAXN];
void pre_sum(ll d[])
{
for(int i=1;i<=n;i++)
{
d[i]+=d[i-1];
d[i]%=mod;
}
}
int main()
{
scanf("%d%d",&n, &q);
for(int i=0;i<q;i++)
{
int type,pos;
scanf("%d%d", &type, &pos);
if(type==1) d1[pos]++;
if(type==2) d2[pos]++;
if(type==3)
{
d3[pos]++;
d3[pos+1]++;
}
}
pre_sum(d3); pre_sum(d3); pre_sum(d3);
pre_sum(d2); pre_sum(d2);
pre_sum(d1);
for(int i=1;i<=n;i++)
{
i==1?printf("%lld", (d1[i]+d2[i]+d3[i])%mod):printf(" %lld", (d1[i]+d2[i]+d3[i])%mod);
}
return 0;
}
G.天哥的序列