Niu Ke Practice 70

A-rearrange

Dichotomize the answer, and then cycle one side.

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int cnt[30];
char s[N];
char p[11]="puleyaknoi";
int n;
bool check(int len)
{
    
    
    if(len>n) return 1;
    memset(cnt,0,sizeof cnt);
    int now=0;
    for(int i=1;i<=len;i++)
        if(s[i]=='p'||s[i]=='u'||s[i]=='l'||s[i]=='e'||s[i]=='y'||s[i]=='a'||s[i]=='k'||s[i]=='n'||s[i]=='o'||s[i]=='i')
        {
    
    
            if(!cnt[s[i]-'a']) now++;
            cnt[s[i]-'a']++;
        }
    if(now==10) return 1;
    for(int i=len+1;i<=n;i++)
    {
    
    
        if(s[i-len]=='p'||s[i-len]=='u'||s[i-len]=='l'||s[i-len]=='e'||s[i-len]=='y'||s[i-len]=='a'||s[i-len]=='k'||s[i-len]=='n'||s[i-len]=='o'||s[i-len]=='i')
        {
    
    
            if(cnt[s[i-len]-'a']==1) now--;
            cnt[s[i-len]-'a']--;
        }
        if(s[i]=='p'||s[i]=='u'||s[i]=='l'||s[i]=='e'||s[i]=='y'||s[i]=='a'||s[i]=='k'||s[i]=='n'||s[i]=='o'||s[i]=='i')
        {
    
    
            if(!cnt[s[i]-'a']) now++;
            cnt[s[i]-'a']++;
        }
        if(now==10) return 1;
    }
    return 0;
}
int main()
{
    
    
    IO;
    int T=1;
    cin>>T;
    while(T--)
    {
    
    
        cin>>s+1;
        n=strlen(s+1);
        int l=10,r=n+1;
        while(l<r)
        {
    
    
            int mid=l+r>>1;
            if(check(mid)) r=mid;
            else l=mid+1;
        }
        if(r==n+1) cout<<-1<<'\n';
        else cout<<l<<'\n';
    }
    return 0;
    
}

B-Patchwork

Sequential automata

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
char s[N];
int ne[N][30];
char p[11]="puleyaknoi";
int n;
void build()
{
    
    
    memset(ne,0,sizeof ne);
    for(int i=n;i;i--)
    {
    
    
        for(int j=0;j<26;j++)
            ne[i-1][j]=ne[i][j];
        ne[i-1][s[i]-'a']=i;
    }
}
int main()
{
    
    
    IO;
    int T=1;
    cin>>T;
    while(T--)
    {
    
    
        cin>>s+1;
        n=strlen(s+1);
        build();
        int res=n;
        for(int i=1;i<=n;i++)
        {
    
    
            if(s[i]==p[0])
            {
    
    
                int now=i;
                for(int j=1;j<10;j++)
                {
    
    
                    now=ne[now][p[j]-'a'];
                    if(!now) break;
                }
                if(now) res=min(res,now-i+1);
            }
        }
        
        if(res!=n) cout<<res<<'\n';
        else cout<<-1<<'\n';
    }
    return 0;
    
}

C-Mu function

The table shows that it is basically a value in the end, just need to make the very large k directly into a smaller number.
Note that the odd or even number of k should not be changed.

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int N=10000010;
int prime[N],cnt;
bool st[N];
int mob[N];
void init(int n)
{
    
    
    mob[1]=1;
    for(int i=2;i<=n;i++)
    {
    
    
        if(!st[i]) prime[++cnt]=i,mob[i]=-1;
        for(int j=1;prime[j]<=n/i;j++)
        {
    
    
            st[i*prime[j]]=1;
            if(i%prime[j]==0) 
            {
    
    
                mob[i*prime[j]]=0;
                break;
            }
            mob[i*prime[j]]=-mob[i];
        }
    }
    
}
int main()
{
    
    
    IO;
    int T=1;
    cin>>T;
    init(10000000);
    while(T--)
    {
    
    
        int n;
        ll k;
        cin>>n>>k;
        if(k>100ll)
        {
    
    
            if(k&1) k=101;
            else k=100;
        }
        for(int i=1;i<=k;i++) n=n+mob[n];
            
        cout<<n<<'\n';
        
    }
    return 0;
}

D-number tree

Note that deleting an edge means deleting all edges, not one of them . The comment code is the code to delete an edge. The number of
connected points minus the number of edges is the answer.
Because the tree has n-1 edges and n points. If there are k trees, then there must be k more points than edges.

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
map<pii,int> mp;
map<int,int> d;
int main()
{
    
    
    IO;
    int T=1;
    //cin>>T;
    while(T--)
    {
    
    
        int n;
        cin>>n;
        int cnte=0,cntv=0;
        while(n--)
        {
    
    
            int op,u,v;
            cin>>op;
            if(op==1)
            {
    
    
                cin>>u>>v;
                if(u>v) swap(u,v);
                // if(!mp[{u,v}]) cnte++;
                // mp[{u,v}]++;
                // if(!d[u]) cntv++;
                // if(!d[v]) cntv++;
                // d[u]++,d[v]++;
                if(mp[{
    
    u,v}]) continue;
                cnte++;
                if(!d[u]) cntv++;
                if(!d[v]) cntv++;
                d[u]++,d[v]++,mp[{
    
    u,v}]=1;
                
            }
            else if(op==2)
            {
    
    
                cin>>u>>v;
                if(u>v) swap(u,v);
                if(!mp[{
    
    u,v}]) continue;
                
                // if(mp[{u,v}]==1) cnte--;
                // mp[{u,v}]--;
                // d[u]--,d[v]--;
                // if(!d[u]) cntv--;
                // if(!d[v]) cntv--;
                cnte--;
                if(d[u]==1) cntv--;
                if(d[v]==1) cntv--;
                d[u]--,d[v]--,mp[{
    
    u,v}]=0;
            }
            else
                cout<<cntv-cnte<<'\n';
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/Fighting_Peter/article/details/108898456