zoukankan      html  css  js  c++  java
  • CF1342F Make It Ascending

    Make It Ascending

    You are given an array (a) consisting of (n) elements. You may apply several operations (possibly zero) to it.

    During each operation, you choose two indices (i) and (j) ((1 le i, j le n; i e j)), increase (a_j) by (a_i), and remove the (i)-th element from the array (so the indices of all elements to the right to it decrease by (1), and (n) also decreases by (1)).

    Your goal is to make the array a strictly ascending. That is, the condition (a_1 < a_2 < dots < a_n) should hold (where (n) is the resulting size of the array).

    Calculate the minimum number of actions required to make the array strictly ascending.

    (1 le n le 15)

    题解

    https://codeforces.com/blog/entry/76633

    Suppose we don't have any constraints on the order of elements, the resulting array just should not contain any duplicates. Let's build the result one element after another in ascending order, so each element we create is strictly greater than the previous. To create an element, just use some subset of elements and merge them into new element. This process can be efficiently modeled with the following dynamic programming: (dp_{cnt, mask}) is the minimum value of the last element, if we merged all the elements from (mask) into (cnt) ascending numbers. To model transitions, we simply iterate on the mask of elements that will be merged into a new one, and check if its sum is greater than the last element we created. This runs in (O(n3^n)), if we use an efficient way to iterate on all masks that don't intersect with the given mask.

    Okay, how about maintaining the order? When we create an element by merging some elements of the original array, let's choose some position of an element we use in merging and state that all other elements are added to it. Then, to ensure that the result is ascending, the position of this element should be greater than the position of the element we chose while building the previous number. We can add the position we have chosen for the last element to the states of our dynamic programming, so it becomes (dp_{cnt, mask, pos}) — the minimum value of the last element, if we merged the (mask) of elements into (cnt) numbers, and the last element originally had index (pos) in the array.

    Using some greedy optimizations (for example, we should not iterate on the position we are choosing to merge — it can be chosen greedily as the leftmost position after the position of previous element we are taking into consideration), we can make it (O(n^2 3^n)), yet with a small constant factor. To restore the answer, we can maintain the previous values of (mask) and (pos) in each state, since (cnt) just increases by (1) with each transition.

    #define ctz __builtin_ctz
    
    CO int N=15,inf=1e9;
    int a[N],sum[1<<N];
    int dp[N+1][1<<N][N+1];
    pair<int,int> p[N+1][1<<N][N+1];
    
    void real_main(){
    	int n=read<int>();
    	for(int i=0;i<n;++i) read(a[i]);
    	for(int mask=0;mask<1<<n;++mask){
    		sum[mask]=0;
    		for(int i=0;i<n;++i)if(mask>>i&1) sum[mask]+=a[i];
    	}
    	for(int cnt=0;cnt<=n;++cnt)for(int mask=0;mask<1<<n;++mask)
    		for(int pos=0;pos<=n;++pos) dp[cnt][mask][pos]=inf;
    	dp[0][0][0]=0;
    	for(int cnt=0;cnt<n;++cnt)for(int mask=0;mask<1<<n;++mask)
    		for(int pos=0;pos<n;++pos)if(dp[cnt][mask][pos]<inf){
    			int rmask=mask^((1<<n)-1);
    			for(int nmask=rmask;nmask;nmask=(nmask-1)&rmask){
    				if(sum[nmask]<=dp[cnt][mask][pos]) continue;
    				if((nmask>>pos)==0) continue; // one should >= pos
    				int npos=pos+ctz(nmask>>pos)+1;
    				if(dp[cnt+1][mask|nmask][npos]>sum[nmask]){
    					dp[cnt+1][mask|nmask][npos]=sum[nmask];
    					p[cnt+1][mask|nmask][npos]={mask,pos};
    				}
    			}
    		}
    	int acnt=0,apos=0;
    	for(int cnt=0;cnt<=n;++cnt)for(int pos=0;pos<=n;++pos)
    		if(dp[cnt][(1<<n)-1][pos]<inf) acnt=cnt,apos=pos;
    	vector<pair<int,int> > ans;
    	int mask=(1<<n)-1,pos=apos;
    	for(int cnt=acnt;cnt>0;--cnt){
    		int nmask=p[cnt][mask][pos].first;
    		int npos=p[cnt][mask][pos].second;
    		for(int i=0;i<n;++i)if((nmask^mask)>>i&1)
    			if(i!=pos-1) ans.push_back({i,pos-1});
    		mask=nmask,pos=npos;
    	}
    	printf("%zd
    ",ans.size());
    	for(int i=0;i<(int)ans.size();++i){
    		int from=ans[i].first;
    		for(int j=0;j<i;++j) from-=ans[j].first<ans[i].first;
    		int to=ans[i].second;
    		for(int j=0;j<i;++j) to-=ans[j].first<ans[i].second;
    		printf("%d %d
    ",from+1,to+1);
    	}
    }
    int main(){
    	for(int T=read<int>();T--;) real_main();
    	return 0;
    }
    
  • 相关阅读:
    Fix the Package System is Broken error in Ubuntu
    linux源镜像网站
    VS2010快捷键大全
    自定义函数与存储过程的比较
    vbcr,vblf和 vbcrlf之间的区别?
    GridView序号问题
    Outlook2007设置备份账号、联系人和个性签名的方法
    c#.net4.0利用odac连接oracle取数
    无法打开登录所请求的数据库DbName 。登录失败。 用户 'IIS APPPOOL\DefaultAppPool' 登录失败。 的解决方案
    C#开发和调用Web Service
  • 原文地址:https://www.cnblogs.com/autoint/p/12849243.html
Copyright © 2011-2022 走看看