天天看点

Yandex.Algorithm 2016 Online Round 2 题解(待补)Role DistributionBinary Representation GameMilk Products

  • Role Distribution
  • Binary Representation Game
  • Milk Products

Role Distribution

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=,f=; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
    while(isdigit(ch)) { x=x*+ch-'0'; ch=getchar();}
    return x*f;
} 
int n,m,a[];
int b[]={};
char s[];
int main()
{
//  freopen("A.in","r",stdin);
//  freopen(".out","w",stdout);
    memset(a,-,sizeof(a));
    cin>>n>>m>>(s+);
    For(i,n) b[s[i]]++; 
    For(i,m) {
        int len=read(),c=read();
        cin>>(s+);
        For(j,len) {
            if (a[s[j]]==-||a[s[j]]>c) a[s[j]]=c;
        }
    }
    ll ans=;
    Fork(i,'a','z') {
        if (b[i]) {
            if (a[i]==-) {
                puts("-1"); return ;
            } 
            ans+=a[i]*b[i];
        }
    }
    cout<<ans<<endl;


    return ;
}
           

Binary Representation Game

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=,f=; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
    while(isdigit(ch)) { x=x*+ch-'0'; ch=getchar();}
    return x*f;
} 
int main()
{
//  freopen("B.in","r",stdin);
//  freopen(".out","w",stdout);
    ll l=read(),r=read(),ans=;
    for(ll i=;i<;i++) {
        ll k=<<(ll)i;
        ll t1=l/k*k;
        ll t2=r/k*k;
        if (t1==t2) ans|=(l&k);
        else {
            ll p[]={};
            p[(bool)(l&k)]+=t1+k-l;
            p[(bool)(r&k)]+=r-t2+;
            ll len=(t2-t1-k)/k;
            int st=(bool)(l&k);
            p[st^]+=k*(len-len/);
            p[st]+=k*(len/);
            if (p[]>p[]) ans|=k;

        } 
    }
    cout<<ans<<endl;


    return ;
}
           

Milk Products

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=,f=; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
    while(isdigit(ch)) { x=x*+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (500010)
int p[MAXN],tot;
bool b[MAXN]={};
void make_prime(int n)
{
    tot=;
    Fork(i,,n)
    {
        if (!b[i]) p[++tot]=i;
        For(j,tot)
        {
            if (i*p[j]>n) break;
            b[i*p[j]]=;
            if (i%p[j]==) break;  
        }
    }
}

map<vi,ll> f;
int n;
ll dfs(vi now) {
    map<vi,ll>::iterator it;
    it = f.find(now);
    if (it!=f.end()) return it->se;
    ll ans=;
    Rep(i,n) ans+=now[i];
    if (now[n-]==) return ans;
    For(i,tot) {
        if (p[i]>now[n-]) break;
        ll an=;
        vi now2;
        Rep(j,n) {
            now2.pb(now[j]/p[i]);
            an+=now[j]%p[i];
        }
        an+=dfs(now2);
        ans=min(an,ans);
    }
    return f[now]=ans;


}
int main()
{
//  freopen("C.in","r",stdin);
//  freopen(".out","w",stdout);
    make_prime();
    cin>>n;
    vi now;
    For(i,n) now.pb(read());
    sort(now.begin(),now.end());
    cout<<dfs(now)<<endl;   

    return ;
}