题目链接

G.做题

题意:


题解:

AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=5e5+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

ll a[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ll n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+1+n);
    ll ans=0;
    for(int i=1;i<=n;i++){
        if(k>=a[i])ans++,k-=a[i];
        if(k<=0)break;
    }
    cout<<ans;
    return 0;
}

F.斗兽棋

题意:



题解:

AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=5e5+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

map<string,int> m;
int g[4][4]={
    1,1,1,0,
    0,1,1,1,
    1,0,1,1,
    1,1,0,1
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    m["elephant"]=1,m["tiger"]=2,m["cat"]=3,m["mouse"]=4;
    string s1,s2;
    cin>>s1>>s2;
    if(g[m[s1]-1][m[s2]-1])cout<<"tiangou yiwusuoyou"<<endl;
    else cout<<"tiangou txdy"<<endl;
    return 0;
}

B.组队

题意:



题解:


AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=5e5+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

ll a[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        for(int i=1;i<=n;i++)cin>>a[i];
        sort(a+1,a+1+n);
        int ans=0;
        for(int i=1;i<=n;i++){
            int p=upper_bound(a+1,a+1+n,k+a[i])-a-1;
            ans=max(ans,p-i+1);
        }
        cout<<ans<<endl;
    }
    return 0;
}

J.建设道路

题意:




题解:






AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

ll a[maxn];

int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    scanf("%d",&n);
    ll sum=0;
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum+=a[i]%mod;
    ll ans=0;
    for(int i=1;i<=n;i++){
        sum=(sum-a[i]+mod)%mod;
        ans=(ans+(n-1)*a[i]%mod*a[i]%mod)%mod;
        ans=(ans-2*a[i]%mod*sum%mod+mod)%mod;
    }
    printf("%lld",ans);
    return 0;
}

I.求和

题意:




题解:







AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

int l[maxn],r[maxn],cnt,a[maxn];
ll c[maxn];
//树状数组
int lowbit(int x)//返还x的最低位
{
    return x&(-x);
}
ll query(int x)
{
    ll s=0;
    while(x>0)
    {
        s+=c[x];
        x-=lowbit(x);
    }
    return s;
}
void add(int x,ll v)
{
    while(x<maxn)
    {
        c[x]+=v;
        x+=lowbit(x);
    }
}
vector<int> g[maxn];
void dfs(int u,int ***]=++cnt;
    for(auto v:g[u]){
        if(v==fa)continue;
        dfs(v,u);
    }
    r[u]=cnt;
}

int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(k,0);
    for(int i=1;i<=n;i++)
        add(l[i],a[i]);
    while(m--){
        int op;
        scanf("%d",&op);
        if(op==1){
            int a,x;
            scanf("%d%d",&a,&x);
            add(l[a],x);
        }
        else{
            int a;
            scanf("%d",&a);
            printf("%d\n",query(r[a])-query(l[a]-1));
        }
    }

    return 0;
}

H.认认都是好朋友

题意:






题解:






AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=2e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
int a[maxn],b[maxn],c[maxn];
int d[maxn<<1],fa[maxn<<1];
int find(int x){
    if(fa[x]==x)return x;
    return fa[x]=find(fa[x]);
}

int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t;
    t=read();
    while(t--){
        int n;
        n=read();
        for(int i=1;i<=n;i++){
            a[i]=read(),b[i]=read(),c[i]=read();
            d[2*i-1]=a[i],d[2*i]=b[i];
            fa[2*i-1]=2*i-1,fa[2*i]=2*i;
        }
        sort(d+1,d+1+2*n);
        int len=unique(d+1,d+1+2*n)-d-1;
        bool f=0;
        for(int i=1;i<=n;i++){
            int x=lower_bound(d+1,d+1+len,a[i])-d;
            int y=lower_bound(d+1,d+1+len,b[i])-d;
            int p=find(x),q=find(y);
            if(c[i])
                fa[q]=p;
        }
        for(int i=1;i<=n;i++){
            int x=lower_bound(d+1,d+1+len,a[i])-d;
            int y=lower_bound(d+1,d+1+len,b[i])-d;
            int p=find(x),q=find(y);
            if(!c[i]&&p==q)f=1;
        }
        if(!f)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}