2019.7.31CodeChef brush title record

2019.7.31CodeChef do title record

Today, the title cut, is not difficult on several road CodeChef, but a good practice code. Cool.

T1

Portal: XENTASK

Flood problem, the accumulated xanny odd-numbered and even-numbered yana SUM1 as $ $ xanny accumulated even-numbered and odd-numbered yana as $ $ SUM2 like small output of both comparison provoke qwq.

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int N=2e4+5;
int T,n,a;
long long sum1,sum2;

inline int get()
{
    int res=0;char c=getchar();
    while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9') res=res*10+c-'0',c=getchar();
    return res;
}

int main()
{
    T=get();
    while(T--)
    {
        n=get();
        sum1=sum2=0;
        for(int i=1;i<=n;i++)
        {
            a=get();
            if(i%2) sum1+=a;
            else sum2+=a;
        }
        for(int i=1;i<=n;i++)
        {
            a=get();
            if(i%2) sum2+=a;
            else sum1+=a;
        }
        cout<<min(sum1,sum2)<<endl;
    }
    return 0;
}

T2

Portal:

EXTRAN

Flood problem. Sequence from small to large, find duplicate or separated output that can provoke qwq

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=1e5+5;
int T,n,a[N];

inline int get()
{
    int res=0;char c=getchar();
    while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9') res=res*10+c-'0',c=getchar();
    return res;
}

int main()
{
    T=get();
    while(T--)
    {
        n=get();
        for(int i=1;i<=n;i++) a[i]=get();
        sort(a+1,a+n+1);
        for(int i=1;i<=n;i++)
        {
            if(a[i]==a[i+1])
            {
                printf("%d\n",a[i]);
                break;
            }
            if(a[i+1]!=a[i]+1)
            {
                if(i==1) printf("%d\n",a[i]);
                else printf("%d\n",a[i+1]);
                break;
            }
        }
    }
    return 0;
}

T3

Portal:

SCHEDULE

A bit difficult qwq

Storing the first character string 01 string, open all the successive record length of an array of blocks. Japanese sentence to be able to modify all $ k $ 0 or 1 and, if applicable, a direct output. Otherwise, half of the maximum number of consecutive days of continuous scanning length of each block, if we are to ensure that the maximum number of consecutive days of no more than mid, which means if there is more than mid-length block, this block is mid length several times, it is necessary use several transform operation. Each communication block number is consumed $ len / mid $ times, then $ res + = len / mid $. After the scan, if the $ res> k $ is the number of days is not legitimate.

#include<bits/stdc++.h>
using namespace std;
char a[1000010];
int b[1000010],cnt,k,n;
bool check(int x)
{
    int s=0,i;
    for(i=1; i<=cnt; i++)
        s+=b[i]/(x+1);
    return s<=k;
}
int main()
{
    int t,i,l,r,ans,mid,x;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%s",&n,&k,a+1);
        cnt=1;
        b[1]=1;
        for(i=1; i<=n; i++)
        {
            if(a[i]==a[i-1]) b[cnt]++;
            else
            {
                cnt++;
                b[cnt]=1;
            }
        }
        x=0;
        for(i=1; i<=n; i++)
            if(a[i]-'0'==(i&1)) x++;
        if(x<=k||(n-x)<=k)
        {
            printf("1\n");
            continue;
        }
        l=2;
        r=n;
        ans=0;
        while(l<=r)
        {
            mid=(l+r)/2;
            if(check(mid)) r=mid-1,ans=mid;
            else l=mid+1;
        }
        printf("%d\n", ans);
    }
    return 0;
}

T4

Portal:

DISHOWN

woc disjoint-set title naked! But there must pay attention to, is put a small value to the large value and above.

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;

const int N=1e4+5;
int n,T,q,a[N],fa[N];

int find(int x)
{
    if(x==fa[x]) return fa[x];
    return fa[x]=find(fa[x]);
}

int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    int o,x,y;
    cin>>T;
    while (T--)
    {
        cin>>n;
        for (int i = 1; i <= n; i++) cin>>a[i];
        cin>>q;
        for (int i = 1; i <= n; i++) fa[i]=i;
        while (q--)
        {
            cin>>o;
            if(o)
            {
                cin>>x;
                cout<<find(x)<<endl;
                continue;
            }
            else
            {
                cin>>x>>y;
                int fx=find(x),fy=find(y);
                if (fx==fy)
                {
                    cout<<"Invalid query!"<<endl;
                    continue;
                }
                else
                {
                    if (a[fx]>a[fy]) fa[fy]=fx;
                    else if (a[fx]==a[fy]) continue;
                    else fa[fx]=fy;
                }
            }
        }
    }
    return 0;
}

T5

Portal:

SUMQ

Difficult qwq, but it is not hard, patiently pushed formula probably will be able to get.

First, the three sequences from small to large. If the $ x_i> y_j $ and $ x_ {i-1} \ geq y_j $, then obviously $ x_ {1 ... i-1} $ less than $ y_j $, same, same is true for $ z_i $.

We set $ mx_i, mz_i $ $ Y $ represent when taken $ y_i $, $ x $ and $ z $ takes a maximum value index.

Push formula, available
$$
ANS = \ Sigma_. 1} ^ {I = {Q} (\ Sigma_. 1} = {J} ^ {mz_j z_i + mz_i y_i) (\ Sigma_. 1} = {J} ^ {MX_j * y_i mx_i +)
$$
with the prefix and maintenance can be.

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;

const int N=100005;
const int mod=1000000007;
long long t,p,q,r,ans,x[N],y[N],z[N],mx[N],mz[N];

inline long long get()
{
    long long res=0;char c = getchar();
    while (c < '0'||c > '9') c = getchar();
    while (c >= '0'&&c <= '9') res = res * 10 + c - '0',c = getchar();
    return res;
}

int main(int argc, char const *argv[])
{
    t=get();
    while (t--)
    {
        ans=0;
        p=get(), q=get(), r=get();
        for (int i = 1; i <= p; i++) x[i] = get();
        for (int i = 1; i <= q; i++) y[i] = get();
        for (int i = 1; i <= r; i++) z[i] = get();
        sort (x+1 ,x+p+1);
        sort (y+1 ,y+q+1);
        sort (z+1 ,z+r+1);
        for (int i = 1; i <= p; i++) x[i] += x[i-1];
        for (int i = 1; i <= q; i++) y[i] += y[i-1];
        for (int i = 1; i <= r; i++) z[i] += z[i-1];
        for (int i = 1; i <= q; i++) mx[i]=p,mz[i]=r;
        for (int i = 1; i <= q; i++)
        {
            for (int j = 1; j <= p; j++)
                if (x[j] - x[j-1] > y[i]) mx[i] = j;
            for (int j = 1; j <= r; j++)
                if (z[j] - z[j-1] > y[i]) mz[i] = j;
        }
        for (int i = 1; i <= q; i++)
        {
            ans=(ans + ((mz[i] * y[i] + z[mz[i]]) * (x[mx[i]] + mx[i] * y[i])) % mod) % mod;
            cout<<ans<<endl;
        }
            
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/zxj-hans/p/11278828.html