2019 cattle off the National Day day3-G & CF1238E

Cattle off G:

Given an array A of size N [], the group M given relationship, so you rearrangement A [], so that the difference in the absolute sum {M} minimum team relationship. First, a sort , followed by a fill in the array.

Suppose there are x i in a binary 1, with DP [i] Update dp [i | (1 << j)], it indicates that the a [x + 1] fill in the j-th position. Noticed a [] already sorted, then a [x] contribution is: + before the number of filled * a [x] - Motian number * a [x];

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=20;
int a[N],p[N],e[N];
ll f[1<<N];
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF) {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n); //重排,然后依次填入
        for(int i=0;i<n;i++) {
            e[i]=0;
            p[i]=0;
        }
        for(int i=0;i<m;i++) {
            int a,b;
            scanf("%d%d",&a,&b);
            a--;b--;
            p[a]|=1<<b;p[b]|=1<<a;
            E [A] ++, E [B] ++ ; 
        } 
        F [ 0 ] = 0 ;
         for ( int I = . 1 ; I <( . 1 << n-); I ++ ) { 
            F [I] = 1e12;
             int K __ builtin_popcount = (I) - . 1 ; it represents a mapping // a [k]
             for ( int J = 0 ; J <n-; J ++ ) {
                 IF (I & ( . 1 << J)) {// represents a [k ] posj fill in the 
                    F [I] = min (F [I], F [I ^ ( . 1<<j)]+1LL*a[k]*(__builtin_popcount(p[j]&i)*2-e[j]));
                }
            }
        }
        printf("%lld\n",f[(1<<n)-1]);
    }
    return 0;
}

 

CF-E:

The meaning of problems: the relationship between a given team M (ai, bi), so you rearrangement, the positional relationship such that {} minimum sum of absolute values.

Enumeration is the last one who still fill, then this is a contribution to meet the (ai already filled, bi not filled) number.

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=2000010;
char c[maxn]; int dp[maxn],f[30][30];
int main()
{
    int N,M;
    scanf("%d%d%s",&N,&M,c+1);
    memset(dp,0x3f,sizeof(dp));
    rep(i,1,N-1) f[c[i]-'a'][c[i+1]-'a']++,f[c[i+1]-'a'][c[i]-'a']++;
    dp[0]=0;
    rep(i,0,(1<<M)-1){
        int t=0;
        rep(j,0,M-1) {
            if(i&(1<<j)){
                rep(k,0,M-1)
                 if(!(i&(1<<k))) t+=f[j][k];
            }
        }
        rep(j,0,M-1) if(!(i&(1<<j))) dp[i|(1<<j)]=min(dp[i|(1<<j)],dp[i]+t);
    }
    printf("%d\n",dp[(1<<M)-1]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hua-dong/p/11641836.html