1. 程式人生 > >第二次組隊賽(xiao)codeforces1070

第二次組隊賽(xiao)codeforces1070

K. Video Posts

Input

The first line contains two integers n and k (1≤k≤n≤105). The next line contains n positive integer numbers a1,a2,…,an (1≤ai≤104), where ai is the duration of the i-th video.

Output

If solution exists, print "Yes" in the first line. Print k positive integers s1,s2,…,sk (s1+s2+⋯+sk=n) in the second line. The total duration of videos in each post should be the same. It can be easily proven that the answer is unique (if it exists).

If there is no solution, print a single line "No".

Examples

Input

6 3
3 3 1 4 1 6

Output

Yes
2 3 1 

Input

3 3
1 1 1

Output

Yes
1 1 1 

Input

3 3
1 1 2

Output

No

Input

3 1
1 10 100

Output

Yes
3 

題意:就是看能否均分,是的話就Yes,然後輸出可以由幾個陣列成,不是就No

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
map<int,int>mp;
map<int,int>::iterator it;
ll c[100010],a[101010];
int main()
{
    ll n,k,i,j,s=0,ss;
    cin>>n>>k;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        s+=a[i];
    }
    if(s%k)
        printf("No\n");
    else
    {
        ss=s/k;
        ll e=0,ans=0,p=0;
        memset(c,0,sizeof(c));
        for(i=0; i<n; i++)
        {
            ans+=a[i];
            p++;
            if(ans==ss)
            {
                c[e++]=p;
                ans=0;
                p=0;
            }
        }
        //cout<<e<<endl;
        if(e==k)
        {
            cout<<"Yes"<<endl;
            cout<<c[0];
            for(i=1; i<e; i++)
                cout<<" "<<c[i];
            cout<<endl;
        }
        else
            cout<<"No"<<endl;
    }

    return 0;
}

D. Garbage Disposal

Examples

Input

3 2
3 2 1

Output

3

Input

5 1
1000000000 1000000000 1000000000 1000000000 1000000000

Output

5000000000

Input

3 2
1 0 1

Output

2

Input

4 4
2 8 4 1

Output

4

題意:扔垃圾,垃圾最多隔一天扔,問最少需要幾個垃圾袋

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
const int A=1e9+7;
ll a[201010];
int main()
{
    ll n,k,i,m,ans=0,j,x=0;
    cin>>n>>m;
    for(i=0;i<n;i++)
        cin>>a[i];
    for(i=0;i<n-1;i++)
    {
        if(a[i]+x>=m)
        {
            ans+=(a[i]+x)/m;
            x=(a[i]+x)%m;
        }
        else
        {
            if(x==0)
                x=a[i];
            else
            {
                x=0;
                ans++;
            }
        }
    }
    if((a[n-1]+x)%m==0)
        ans+=(a[n-1]+x)/m;
    else
        ans+=(a[n-1]+x)/m+1;
    cout<<ans<<endl;
    return 0;
}

F. Debate

Elections in Berland are coming. There are only two candidates — Alice and Bob.

The main Berland TV channel plans to show political debates. There are n people who want to take part in the debate as a spectator. Each person is described by their influence and political views. There are four kinds of political views:

  • supporting none of candidates (this kind is denoted as "00"),
  • supporting Alice but not Bob (this kind is denoted as "10"),
  • supporting Bob but not Alice (this kind is denoted as "01"),
  • supporting both candidates (this kind is denoted as "11").

The direction of the TV channel wants to invite some of these people to the debate. The set of invited spectators should satisfy three conditions:

  • at least half of spectators support Alice (i.e. 2⋅a≥m, where a is number of spectators supporting Alice and mis the total number of spectators),
  • at least half of spectators support Bob (i.e. 2⋅b≥m, where b is number of spectators supporting Bob and m is the total number of spectators),
  • the total influence of spectators is maximal possible.

Help the TV channel direction to select such non-empty set of spectators, or tell that this is impossible.

Input

The first line contains integer n(1≤n≤4⋅105) — the number of people who want to take part in the debate as a spectator.

These people are described on the next n lines. Each line describes a single person and contains the string si and integer ai separated by space (1≤ai≤5000), where si denotes person's political views (possible values — "00", "10", "01", "11") and ai — the influence of the i-th person.

Output

Print a single integer — maximal possible total influence of a set of spectators so that at least half of them support Alice and at least half of them support Bob. If it is impossible print 0 instead.

  • 6
    11 6
    10 4
    01 3
    00 3
    00 7
    00 9
    Output
    22
    
    Input
    5
    11 1
    01 1
    00 100
    10 1
    01 1
    
    Output
    103
    
    Input
    6
    11 19
    10 22
    00 18
    00 29
    11 29
    10 28
    
    Output
    105
    
    Input
    3
    00 5000
    00 5000
    00 5000
    
    Output
    0
    

    Note

    In the first example 4

    spectators can be invited to maximize total influence: 1, 2, 3 and 6. Their political views are: "11", "10", "01" and "00". So in total 2 out of 4 spectators support Alice and 2 out of 4 spectators support Bob. The total influence is 6+4+3+9=22.

    In the second example the direction can select all the people except the 5-th person.

    In the third example the direction can select people with indices: 1, 4, 5 and 6.

    In the fourth example it is impossible to select any non-empty set of spectators.

    題意:兩個人要進行辯論賽,劇場要選觀眾,所選的觀眾要滿足3個條件,至少有一半的人支援第一個人,至少有一半的人支援另一個人,所選觀眾影響力最大,問你最大的影響力是多少。

思路:11的肯定要,在01跟10的情況下找最小的是都要的 ,然後在剩下的一些中找最大的,先判斷還需要幾個人

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
const int AA=4e5+7;
struct A
{
    ll p;
    ll q;
}s1[AA],s2[AA],s3[AA],s4[AA];
bool cmp(struct A l,struct A ll)
{
    if(l.p==ll.p)
        return l.q>ll.q;
    else
        return l.p>ll.p;
}
bool cmp1(struct A l,struct A ll)
{
    return l.q>ll.q;
}
int main()
{
    ll a,ans=0,b,i,j=0,k=0,t=0,tt=0,m=0,sum=0,n;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a>>b;
        if(a==11)
        {
            s1[j].p=a;
            s1[j].q=b;
            j++;
        }
        if(a==10)
        {
            s2[k].p=a;
            s2[k].q=b;
            k++;
        }
        if(a==1)
        {
            s3[t].p=a;
            s3[t].q=b;
            t++;
        }
        if(a==0)
        {
            s4[tt].p=a;
            s4[tt].q=b;
            tt++;
        }
    }
    for(i=0;i<j;i++)
    {
        ans+=s1[i].q;
    }
    sort(s2,s2+k,cmp);
    sort(s3,s3+t,cmp);
    sort(s4,s4+tt,cmp);
    //cout<<j<<" "<<k<<" "<<t<<" "<<tt<<endl;
    a=b=j;
    sum+=j;
    //cout<<a<<" "<<b<<" "<<sum<<" "<<ans<<endl;
    if(k==t)
    {
        for(i=0;i<k;i++)
        {
            ans+=s2[i].q;
            ans+=s3[i].q;
        }
        a+=k;
        b+=k;
        sum+=k*2;
        //cout<<a<<" "<<b<<" "<<sum<<" "<<ans<<endl;
        sort(s4,s4+tt,cmp);
        for(i=0;i<tt;i++)
        {
            if(2*a-sum<=0)
                break;
            ans+=s4[i].q;
            sum++;
        }
    }
    else
    if(k>t)
    {
        //cout<<t<<endl;
        //cout<<s2[0].q<<" "<<s3[0].q<<endl;
        for(i=0;i<t;i++)
        {
            ans+=s2[i].q;
            ans+=s3[i].q;
        }
        a+=t;
        b+=t;
        sum+=t*2;
        //cout<<a<<" "<<b<<" "<<sum<<" "<<ans<<endl;
        if(2*a-sum==0)
        {
            cout<<ans<<endl;
            return 0;
        }
        for(i=t;i<k;i++)
        {
            s4[tt].p=10;
            s4[tt].q=s2[i].q;
            tt++;
        }
        sort(s4,s4+tt,cmp1);
        for(i=0;i<tt;i++)
        {
            if(2*a-sum<=0)
                break;
            ans+=s4[i].q;
            sum++;
        }
    }
    else
    if(k<t)
    {
        for(i=0;i<k;i++)
        {
            ans+=s2[i].q;
            ans+=s3[i].q;
        }
        a+=k;
        b+=k;
        sum+=k*2;
        if(2*a-sum==0)
        {
            cout<<ans<<endl;
            return 0;
        }
        //cout<<a<<" "<<b<<" "<<sum<<" "<<ans<<endl;
        for(i=k;i<t;i++)
        {
            s4[tt].p=01;
            s4[tt].q=s3[i].q;
            tt++;
        }
        sort(s4,s4+tt,cmp1);
        for(i=0;i<tt;i++)
        {
            if(2*a-sum<=0)
                break;
            ans+=s4[i].q;
            sum++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

H - BerOS File Suggestion

 Input

4
test
contests
test.
.test
6
ts
.
st.
.test
contes.
st

Output

1 contests
2 test.
1 test.
1 .test
0 -
4 .test

題意:輸出出現過幾次然後隨便輸出一個滿足的主串

思路:直接找的話,會時間超限,所以可以用map把上面輸入的串的所有字串都記下來,用兩個map,一個記錄有幾個串有匹配的,一個記錄匹配的串是哪個。單個去重,總的不去重。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
const int AA=4e5+7;
map<string,ll> M;
map<string,string> N;
int main()
{
    ll n,ans=0,i,j,m,k;
    string s;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>s;
        ll len=s.size();
        map<string,ll> qc;
        for(j=0;j<len;j++)
        {
            for(k=1;k<=len;k++)
            {
                string t;
                t=s.substr(j,k);
                if(qc[t]==0)
                {
                    M[t]++;
                    N[t]=s;
                    qc[t]=1;
                }
            }
        }
    }
    cin>>m;
    string ss;
    for(i=0;i<m;i++)
    {
        cin>>ss;
        if(M[ss]==0)
            cout<<"0 -"<<endl;
        else
            cout<<M[ss]<<" "<<N[ss]<<endl;
    }
    return 0;
}