More than 2019 cattle off summer school camp (second field)

A:

This is a funny goose problem

Up a teammate guess the conclusion guessed right

But the wa

Then throw this question the

Finally, I found the violence teammates right conclusion

Then goose found no multiplicative

D:

Find the group seems to be no faster way
can only be violence
this problem, although there are many, many groups
, but we only find a small pre-k-line
problem now is that we can not follow each group of small to large access to
the first method is to use priority queue
we started to throw a single point to
the value of the smallest group to the right each time a pop-up
although we can not guarantee that the queue is the first k k before the current small
but we can guarantee now head of the queue is currently minimal
because state back in the queue than he must
not visit the state of the queue is transferred in the past will be greater
so each team's first pop-up must be right
then to state first team to reach the state he can get queue
like this just need time to pop k ended
every pop-up and expansion -> O (logN the X-+ the n-*)
the X-violence, then where is n, that is, the cycle will tle
then int128 or optimized look like a bitset

The second method is half
back to the previous question
we can not turn from small to large to traverse all of the group
but if we know a lim
We are now looking for the right value <= lim of their group
so that we can search violence
if extended to a legal group would continue
such operations will make k--
if extended to an illegal stop
this will only waste time will not go very far on the road illegally
and this lim there can be half the monotony of
O (log MAX_lim * k * X)
the X is a time to find a new group of maximum time
can be optimized with int128

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#define maxn 110
#define tpc __int128
#define ll long long
using namespace std;
ll n,k,w[maxn];
tpc G[maxn];
struct node{
    ll big,val;
    tpc S;
    bool operator < (const node &A )const{
        return A.val<val;
    }
};
priority_queue<node>q;
char s[maxn][maxn];
int main(){
    scanf("%lld%lld",&n,&k);
    k--;node now,tmp;
    for(ll i=1;i<=n;i++)
        scanf("%lld",&w[i]);
    for(ll i=1;i<=n;i++)
        scanf("%s",s[i]+1);
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++)
            if(s[i][j]=='1')G[i]+=tpc(1)<<j;
        now.val=w[i];now.S=tpc(1)<<i;
        now.big=i;q.push(now);
    }
    now.val=0;
    for(ll i=1;i<=k;i++){
        if(q.empty()){
            now.val=-1;break;
        }
        now=q.top();q.pop();
        for(ll v=now.big+1;v<=n;v++){
            if((G[v]&now.S)==now.S){
                tmp.big=v;tmp.val=now.val+w[v];
                tmp.S=now.S|(tpc(1)<<v);q.push(tmp);
            }
        }
    }
    printf("%lld\n",now.val);
    return 0;
}
View Code
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<bitset>
#include<algorithm>
#define tpc int
#define maxn 110
#define ll long long
using namespace std;
int n,k,cnt;
tpc G[maxn],S;
ll ans,l,r;
struct node{
    int w,o;
}a[maxn];
char s[maxn][maxn];
int cmp(const node &A,const node &B){
    return A.w<B.w;
}
void Dfs(int now,ll val,ll lim){
    int id=a[now].o;
    if(now==n+1||cnt>=k)return;
    if(val+a[now].w>lim)return;
    if((G[id]&S)==S){
        cnt++;S|=tpc(1)<<id;
        Dfs(now+1,val+a[now].w,lim);
        S^=tpc(1)<<id;
    }
    Dfs(now+1,val,lim);
} 
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i].w);a[i].o=i;r+=a[i].w;
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++)
        scanf("%s",s[i]+1);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            if(s[i][j]=='1')G[i]+=tpc(1)<<j;
    }
    if(k<=1){
        printf("0\n");return 0;
    }
    k--;ans=-1;
    while(l<=r){
        ll mid=(l+r)/2;
        cnt=0;S=0;Dfs(1,0,mid);
        if(cnt>=k){
            ans=mid;r=mid-1;
        }
        else l=mid+1;
    }
    printf("%lld\n",ans);
    return 0;
}
/*
    3 6
    3 2 1
    011
    101
    110
    
    
    5 22
    1 2 3 4 5
    01111
    10111
    11011
    11101
    11110
    
    5 22
    5 4 3 2 1
    01111
    10111
    11011
    11101
    11110
*/
View Code

F:

Violence + optimization pruning

ans = val and for all before - between internal teams fight each other val

Val latter is increasing, it can be used to optimize the pruning

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#define maxn 30
#define ll long long
using namespace std;
int n,g[maxn][maxn],A[maxn],B[maxn];
ll ans,sum;
void Dfs(int now,ll s,int a,int b){
    ll res;
    if(now==2*n+1){
        if(ans<sum-s){
            ans=sum-s;//printf("ans : %d\n",ans);
            //for(int i=1;i<=n*2;i++)printf("%d ",S[i]);printf("\n");
        }
        return;
    }
    if(a<b){
        res=0;
        for(int i=1;i<=a;i++)
            res+=g[now][A[i]];
        if(sum-s-res>ans&&a<n){
            A[a+1]=now;Dfs(now+1,s+res,a+1,b);A[a+1]=0;
        }
        res=0;
        for(int i=1;i<=b;i++)
            res+=g[now][B[i]];
        if(sum-s-res>ans&&b<n){
            B[b+1]=now;Dfs(now+1,s+res,a,b+1);B[b+1]=0;
        }   
    }
    else {
        res=0;
        for(int i=1;i<=b;i++)
            res+=g[now][B[i]];
        if(sum-s-res>ans&&b<n){
            B[b+1]=now;Dfs(now+1,s+res,a,b+1);B[b+1]=0;
        }   
        res=0;
        for(int i=1;i<=a;i++)
            res+=g[now][A[i]];
        if(sum-s-res>ans&&a<n){
            A[a+1]=now;Dfs(now+1,s+res,a+1,b);A[a+1]=0;
        }
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n*2;i++)
        for(int j=1;j<=n*2;j++){
            scanf("%d",&g[i][j]);sum+=g[i][j];
        }
    sum/=2;Dfs(1,0,0,0);printf("%lld\n",ans);
    return 0;
}
/*
2
0 1 1000000000 1000000000
1 0 1000000000 1000000000
1000000000 1000000000 0 1
1000000000 1000000000 1 0 
*/
View Code

H:

Delete the enumeration of the largest side of the matrix 1

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1010
using namespace std;
int n,m,h[maxn][maxn],l[maxn][maxn],r[maxn][maxn],ans,x1,y1,x2,y2;
char g[maxn][maxn],t[maxn];
int Solve(int falg){
    memset(h,0,sizeof(h));
    memset(l,0,sizeof(l));
    memset(r,0,sizeof(r));
    int res=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            l[i][j]=r[i][j]=j;
            if(g[i][j]=='0')continue;
            else h[i][j]=h[i-1][j]+1;
        }
//  printf("h:\n");
//  for(int i=1;i<=n;i++){
//      for(int j=1;j<=m;j++)
//          printf("%d ",h[i][j]);
//      printf("\n");
//  }
    for(int i=1;i<=n;i++){
        for(int j=2;j<=m;j++){
            if(g[i][j]=='0')continue;
            while(h[i][l[i][j]-1]>=h[i][j])
                l[i][j]=l[i][l[i][j]-1];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=m-1;j>=1;j--){
            if(g[i][j]=='0')continue;
            while(h[i][r[i][j]+1]>=h[i][j])
                r[i][j]=r[i][r[i][j]+1];
        }
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(res<h[i][j]*(r[i][j]-l[i][j]+1)){
                res=h[i][j]*(r[i][j]-l[i][j]+1);
                if(falg){
                    x1=i-h[i][j]+1;y1=l[i][j];x2=i;y2=r[i][j];
                }
            }
        }
    return res;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%s",g[i]+1);
    Solve(1);
    for(int i=y1;i<=y2;i++){
        t[i]=g[x1][i];g[x1][i]='0';
    }
    ans=max(ans,Solve(0));
    for(int i=y1;i<=y2;i++){
        g[x1][i]=t[i];
    }
     
    for(int i=y1;i<=y2;i++){
        t[i]=g[x2][i];g[x2][i]='0';
    }
    ans=max(ans,Solve(0));
    for(int i=y1;i<=y2;i++){
        g[x2][i]=t[i];
    }
     
    for(int i=x1;i<=x2;i++){
        t[i]=g[i][y1];g[i][y1]='0';
    }
    ans=max(ans,Solve(0));
    for(int i=x1;i<=x2;i++){
        g[i][y1]=t[i];
    }
     
    for(int i=x1;i<=x2;i++){
        t[i]=g[i][y2];g[i][y2]='0';
    }
    ans=max(ans,Solve(0));
    for(int i=x1;i<=x2;i++){
        g[i][y2]=t[i];
    }
     
    printf("%d\n",ans);
    //printf("%d %d %d %d\n",x1,y1,x2,y2);
    return 0;
}
/*
5 6
001010
000110
010111
010111
000110
 
4 5
11011
01010
10110
00111
 
4 9
001110011
001110011
001110011
000000011
 
 
*/
View Code

 

Guess you like

Origin www.cnblogs.com/yanlifneg/p/11220837.html