CF # R582 (div 3) summary

First Tucao about school, why should school at 8.31 ah, playing half the mentality of collapse

On the back, thinking should not be out a lot of .....................................

Mad, mend it solution to a problem (problem solution is subsequently filled, he spent almost two hours)

A - Chips Moving

It is easy to think that this thing can directly count each number is odd or even number of occurrences, whichever is less, to

#include<bits/stdc++.h>
using namespace std;
#define LL long long
int a,n,m,p,q;
int ans;
int main()
{
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        int x;cin>>x;
        if (x&1) p++;
        else q++;
    }
    printf("%d\n",min(p,q));
    return 0;
}

B. Bad Prices

Originally thought is Fenwick tree directly maintain it, but too lazy to write discrete, data discovery hand to play, just look at the current point behind no less than the number it can be, it can scan backwards

 

#include<bits/stdc++.h>
#define lowbit(x) (x&(-x))
using namespace std;
const int N=2e5+7;
int tree[N];
int T;
int ans,m,n;
int a[N],p[N];
int main()
{
    cin>>T;
    while (T--)
    {
        cin>>n;ans=0;
        for (int i=1;i<=n;i++) cin>>a[i];int mn=a[n];
        for (int i=n-1;i>=1;i--)
        {
            if (a[i]>mn) {ans++;}
            else {mn=min(mn,a[i]);continue;}
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

C. Book Reading

Must have some circulation figures for each section, we first deal with a number of cycles he needs for the cycle section can be calculated direct violence

The time to write silly, too lazy to find the law, it is a direct wholly forget it again, as if the prime number can be divided into non-prime number classification (fog

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL ans;
LL m,n,T;
int main()
{
    cin>>T;
    while (T--)
    {
        cin>>n>>m;
        LL p=m%10;ans=p;LL tm=n/m;
        if (p==1)
        {
            ans=(tm/10)*45;
            for (int i=1;i<=tm%10;i++) ans+=i;
        }
        if (p==2)
        {
            ans=(tm/5)*20;
            for (int i=1;i<=tm%5;i++) ans+=i*2;
        }
        if (p==3)
        {
            ans=(tm/10)*(45);LL k=3;
            for (int i=1;i<=tm%10;i++)
            {
                ans+=k;
                k=(k+3)%10;
            }
        }
        if (p==4)
        {
            ans=(tm/5)*(20);LL k=4;
            for (int i=1;i<=tm%5;i++)
            {
                ans+=k;
                k=(k+4)%10;
            }
        }
        if (p==5)
        {
            ans=(tm/2)*(5);LL k=5;
            for (int i=1;i<=tm%2;i++)
            {
                ans+=k;
                k=(k+5)%10;
            }
        }
        if (p==6)
        {
            ans=(tm/5)*(20);LL k=6;
            for (int i=1;i<=tm%5;i++)
            {
                ans+=k;
                k=(k+6)%10;
            }
        }
        if (p==7)
        {
            ans=(tm/10)*(45);LL k=7;
            for (int i=1;i<=tm%10;i++)
            {
                ans+=k;
                k=(k+7)%10;
            }
        }
        if (p==8)
        {
            ans=(tm/5)*(20);LL k=8;
            for (int i=1;i<=tm%5;i++)
            {
                ans+=k;
                k=(k+8)%10;
            }
        }
        if (p==9)
        {
            ans=(tm/10)*(45);LL k=9;
            for (int i=1;i<=tm%10;i++)
            {
                ans+=k;
                k=(k+9)%10;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

D2. Equalizing by Division 

This problem of toxic, can write directly on the test would have been open, I have to use the stack to maintain, I zz, so we have not out of tune, mad person

In fact, we For each value, direct break it down, the number of cloth can be included into an array, and finally to a minimum can scan it again

#include<bits/stdc++.h>
#define I inline
using namespace std;
const int N=2e5+7;
const int INF=0x7f7f7f7f;
vector<int> v[N];
int n,k,a[N],st;
I int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void solve(int x)
{
    int cnt=0;
    while (x)
    {
        v[x].push_back(cnt);
        x>>=1;
        cnt++;
    }
}
int main()
{
    n=read(),k=read();
    for (int i=1;i<=n;i++) a[i]=read();
    for (int i=1;i<=n;i++) solve(a[i]),st=max(st,a[i]);
    int ans=INF;
    for (int i=0;i<=st;i++)
    {
        if (v[i].size()<k) continue;
        sort(v[i].begin(),v[i].end());
        ans=min(ans,accumulate(v[i].begin(),v[i].begin()+k,0));
    }
    cout << years;
    return  0 ; 
}

E. Two Small Strings

Solution to a problem is not clear what is meant by the algorithms themselves YY:

We consider three characters are arranged in each case, only six species, can not look at yourself first in its judgment attached

If not, then the judgment itself to the character of the topics covered, the output of direct violence to himself every character N times

#include<bits/stdc++.h>
using namespace std;
string s[7],sp;
int n,Q;
bool vis[7];
int main()
{
    s[1]="abc",s[2]="acb",s[3]="bca",s[4]="bac",s[5]="cab",s[6]="cba";
    cin>>n;
    string ss[3];
    int pos=0;
    cin>>ss[1]>>ss[2];
    for (int i=1;i<=6;i++)
    {
        if (s[i].find(ss[1])!=-1) vis[i]=true;
        if (s[i].find(ss[2])!=-1) vis[i]=true;
        if (!vis[i]) Q=i;
    }
    if (n==1) {cout<<"YES"<<endl<<s[Q];return 0;}
    pos=0;
    for (int i=1;i<=6;i++) 
    {
        if (vis[i]) continue;
        sp.clear();sp+=s[i][2],sp+=s[i][0];
        if (sp.find(ss[1])!=-1) continue;
        if (sp.find(ss[2])!=-1) continue;
        pos=i;
    }
    if (pos) 
    {
        printf("YES\n");
        for (int i=1;i<=n;i++) cout<<s[pos];
    }
    else 
    {
        printf("YES\n");
        for (int i=1;i<=n;i++) cout<<s[Q][0];
        for (int i=1;i<=n;i++) cout<<s[Q][1];
        for (int i=1;i<=n;i++) cout<<s[Q][2];
    }
    return 0;
} 

F. Unstable String Sort

Gugu Gu nor write, problem solution can not read, awcsl. . . . . .

 

 

G. Path Queries

Long time no write-tree heuristic merger, a title bar, should be considered

First, for each m considered a limit, which limit the sort required for maintenance after each edge disjoint-set

Heuristic merge out, answer directly to statistics, discrete finally come back, you can output

#include<bits/stdc++.h>
#define I inline
#define LL long long
using namespace std;
const int N=2e5+7;
LL res;
int sz[N],n,fa[N],m;
LL calc(LL x)
{
    return 1LL*(x)*(x-1)/2LL;
}
I int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int find(int x)
{
    if (fa[x]!=x) return fa[x]=find(fa[x]);
    return fa[x];
}
struct node
{
    LL pos,ans,id;
} t[N];
struct edge
{
    int from,to,dis;
    bool operator <(const edge &x) const
    {
        return dis<x.dis;
    }
} e[N];
bool cmp1(node a,node b)
{
    return a.pos<b.pos;
}
bool cmp2(node a,node b)
{
    return a.id<b.id;
}
void merge(int x,int y)
{
    int fx=find(x),fy=find(y);
    if (sz[fx]>sz[fy]) swap(fx,fy);
    res-=calc(sz[fx]);res-=calc(sz[fy]);
    sz[fy]+=sz[fx];
    res+=calc(sz[fy]);
    fa[fx]=fy;
}
void solve()
{
    int pos=1;
    for (int i=1;i<=m;i++)
    {
        while (e[pos].dis<=t[i].pos&&pos<=n-1)
        {
            merge(e[pos].from,e[pos].to);
            pos++;
        }
        t[i].ans=res;
    }
}
int main()
{
    n=read();m=read();
    for (int i=1;i<n;i++)
    {
        int x=read(),y=read(),z=read();
        e[i].from=x;e[i].to=y;e[i].dis=z;
    }
    for (int i=1;i<=m;i++)
    {
        t[i].pos=read(),t[i].id=i;
    }
    sort(e+1,e+n);sort(t+1,t+m+1,cmp1);
    for (int i=1;i<=n;i++) fa[i]=i,sz[i]=1;
    solve();
    sort(t+1,t+m+1,cmp2);
    for (int i=1;i<=m;i++) printf("%I64d ",t[i].ans);
    return 0;
}

 

 

The code is purely original, if the code is the same problem with the same idea or solution, as I have no melon, qwq

F who can teach me how to write ah, I was too dishes.

 

Guess you like

Origin www.cnblogs.com/Hale522520/p/11442237.html