Week 8 Team Match

Keywords: Programming

K Video Posts

Link to this question
Title: It depends on whether the score can be equalized. Yes, then the output can be made up of several numbers, not No.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
int a[100010],n,k,b[100010],flag=0;
int check(int x)
{
    int sum=0,j=-1;
    for(int i=0; i<n; i++)
    {
        sum+=a[i];
        if(sum==x)
        {
            sum=0;
            b[flag++]=i-j;
            j=i;
            continue;
        }
        else if(sum>x)
        {
            return 0;
        }
    }
    if(sum!=0)
        return 0;
    return 1;
}
int main()
{
    int sum=0,c=0;
    cin>>n>>k;
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
        sum+=a[i];
    }
    if(k==1)
    {
        cout<<"Yes"<<endl;
        cout<<n<<endl;
        return 0;
    }
    else
    {
        for(int i=1; i<=sum; i++)
        {
            c=0;
            flag=0;
            if(check(i)==1)
            {
                if(flag!=k)
                {
                    continue;
                }
                else
                {
                    c=1;
                    break;
                }
            }
            else
                continue;

        }
    }
    if(c==0)
        cout<<"No"<<endl;
    else
    {
        cout<<"Yes"<<endl;
        for(int i=0; i<flag-1; i++)
        {
            cout<<b[i]<<" ";
        }
        cout<<b[flag-1]<<endl;
    }
    return 0;
}

H - BerOS File Suggestion

Title Link
Give you a string, find out if the substring is the parent string, and if so, output any parent string.
Idea: Find substrings, because the length of known strings is not more than 8, so you can directly find all the substrings of known strings and store them in map.
Define three map s, one to record the number of occurrences of the substring, one to mark whether the substring has been found, and the other to store the parent string of the substring (any one)
Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
map<string,string> ans;
map<string,int> cnt;
map<string,bool> vis;
string s;
char a[10];
int main()
{
    int n,q,len;
    cin>>n;
    while(n--)
    {
        cin>>a;
        vis.clear();
        len=strlen(a);
        for(int i=0;i<len;i++)
        {
            s="";
            for(int j=i;j<len;j++)
            {
                s+=a[j];
                if(!vis[s])                {
                    cnt[s]++;
                    ans[s]=a;
                    vis[s]=true;
                }
            }
        }
    }
    cin>>q;
    while(q--)
    {
        cin>>a;
        if(cnt[a]==0)
            cout<<cnt[a]<<" "<<"-"<<endl;
        else
            cout<<cnt[a]<<" "<<ans[a]<<endl;
    }
    return 0;
}

F - Debate

Title Link
There are two candidates running for election. If the input of 00 is not supported by anyone, 01 is for the latter, 10 is for the former and 11 is for the former, each voter has his own influence. The number of candidates supporting the two candidates must be more than half of the total number to find the greatest influence.

First add all 11, then take the one in the middle of 01 and 10, add 10 and 01, then put the extra 01 or 10 together with 00, and then take the most influential person.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
#define maxn 400010
typedef long long LL;
LL a[maxn],b[maxn],c[maxn],d[maxn],sum;
LL n,v;
char s[10];
int cmp(LL x,LL y)
{
    return x>y;
}
int main()
{
    LL l=0,l1=0,l2=0,l3=0,l4=0,num=0,m=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>s>>v;
        if(strcmp(s,"00")==0)
        {
            a[l1++]=v;
        }
        if(strcmp(s,"10")==0)
        {
            b[l2++]=v;
        }
        if(strcmp(s,"01")==0)
        {
            c[l3++]=v;
        }
        if(strcmp(s,"11")==0)
        {
            l4++;
            sum+=v;
        }
    }
    sort(a,a+l1,cmp);
    sort(b,b+l2,cmp);
    sort(c,c+l3,cmp);
    if(l2<=l3)
    {
        m=l4+l2;
        num+=l4;
        for(int i=0;i<l2;i++)
        {
            sum+=(b[i]+c[i]);
            b[i]=0;
            c[i]=0;
            num+=2;
        }
        while(l1>0)
            d[l++]=a[--l1];
        while(l3>0)
            d[l++]=c[--l3];
    }
    else
    {
        m=l4+l3;
        num+=l4;
        for(int i=0;i<l3;i++)
        {
            sum+=(b[i]+c[i]);
            b[i]=0;
            c[i]=0;
            num+=2;
        }
        while(l1>0)
            d[l++]=a[--l1];
        while(l2>0)
            d[l++]=b[--l2];
    }
    sort(d,d+l,cmp);
    LL t=0;
    while(2*m>num)
    {
        num++;
        sum+=d[t++];
        if(d[t]==0)
            break;
    }
    cout<<sum<<endl;
    return 0;
}

Posted by rajns on Wed, 23 Jan 2019 23:12:13 -0800