Links: https://ac.nowcoder.com/acm/problem/21873
Source: Cattle-off network
Title Description
Taurus a total of m blocks of computer memory, there are n instructions, each instruction is a sequence of 01
if the i-th character is a command, indicating that instruction requires access to memory block i
execution cost of each instruction is k ^ 2, k is the number of new access memory, that is before the instruction does not have access to the amount of memory
you can freely arrange the order of execution of the instruction n, find complete instructions to perform all of the minimum cost
if the i-th character is a command, indicating that instruction requires access to memory block i
execution cost of each instruction is k ^ 2, k is the number of new access memory, that is before the instruction does not have access to the amount of memory
you can freely arrange the order of execution of the instruction n, find complete instructions to perform all of the minimum cost
Enter a description:
The first input line of a first integer n-( . 1 ≤ n- ≤ 20 is 1≤n≤20)
Next per line 01 a string of not more than 20
Output Description:
An integer output
Specific ideas:
DP [i] denotes the i which corresponds to the selected binary lines of minimum cost.
Val [i] denotes the i which corresponds to the selected binary lines corresponding to the memory access which
AC Code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 # define ll long long 4 # define inf 0x3f3f3f3f 5 const int mod = 1e9+7; 6 const int maxn = 2e6+100; 7 int dp[maxn],val[maxn]; 8 int n,m; 9 int cal(char str[]) 10 { 11 int len=strlen(str); 12 int tmp=1; 13 int ans=0; 14 for(int i=len-1; i>=0; i--) 15 { 16 ans+=(str[i]-'0')*tmp; 17 tmp<<=1; 18 } 19 return ans; 20 } 21 char str[25]; 22 int f(int t1,int t2) 23 { 24 int ans=0; 25 for(int i=0; i<=20; i++) 26 { 27 if(((t1&(1<<i))==0)&&(t2&(1<<i))) 28 ans++; 29 } 30 return ans*ans; 31 } 32 int a[maxn]; 33 int main() 34 { 35 memset(dp,inf,sizeof(dp)); 36 dp[0] = 0 ; 37 [ Scanf ( " % D% D " , & n-, & m); 38 is for ( int I = 0 ; I <n-; I ++ ) 39 { 40 Scanf ( " % S " , STR); 41 is A [ I] = CAL (STR); // because when comparing the comparison is a decimal, the first converted into binary 42 is } 43 is int maxstate = ( . 1 << n-) - . 1 ; 44 is for ( int I = 0 ; I < maxstate =; I ++ ) 45 { 46 for(int j=0; j<n; j++) 47 { 48 if(i&(1<<j)) 49 continue; 50 if(dp[i]==inf) 51 continue; 52 int tmp=dp[i]+f(val[i],a[j]); 53 if(dp[i|(1<<j)]>tmp) 54 { 55 dp[i|(1<<j)]=tmp; 56 val[i|(1<<j)]=val[i]|a[j]; 57 } 58 } 59 } 60 printf("%d\n",dp[maxstate]); 61 return 0; 62 }