Thursday, 10 March 2016

Problem Statement

    
Cat Noku has just finished writing his first computer program. Noku's computer has m memory cells. The cells have addresses 0 through m-1. Noku's program consists of n instructions. The instructions have mutually independent effects and therefore they may be executed in any order. The instructions must be executed sequentially (i.e., one after another) and each instruction must be executed exactly once.
You are given a description of the n instructions as a String[] with n elements. Each instruction is a String of m characters. For each i, character i of an instruction is '1' if this instruction accesses memory cell i, or '0' if it does not.
Noku's computer uses caching, which influences the time needed to execute an instruction. More precisely, executing an instruction takes k^2 units of time, where k is the number of new memory cells this instruction accesses. (I.e., k is the number of memory cells that are accessed by this instruction but have not been accessed by any previously executed instruction. Note that k may be zero, in which case the current instruction is indeed executed in 0 units of time.)
Noku's instructions can be executed in many different orders. Clearly, different orders may lead to a different total time of execution. Find and return the shortest amount of time in which it is possible to execute all instructions.
 

Definition

    
Class:OrderOfOperationsDiv2
Method:minTime
Parameters:String[]
Returns:int
Method signature:int minTime(String[] s)
(be sure your method is public)
    
 

Constraints

-n,m will be between 1 and 20, inclusive.
-s will have exactly n elements.
-Each element of s will have exactly m characters.
-Each character of s[i] will be either '0' or '1' for all valid i.
 

Examples

0)
    
{
 "111",
 "001",
 "010"
}
Returns: 3
Cat Noku has 3 instructions. The first instruction ("111") accesses all three memory cells. The second instruction ("001") accesses only memory cell 2. The third instruction ("010") accesses only memory cell 1. If Noku executes these three instructions in the given order, it will take 3^2 + 0^2 + 0^2 = 9 units of time. However, if he executes them in the order "second, third, first", it will take only 1^2 + 1^2 + 1^2 = 3 units of time. This is one optimal solution. Another optimal solution is to execute the instructions in the order "third, second, first".
1)
    
{
 "11101",
 "00111",
 "10101",
 "00000",
 "11000"
}
Returns: 9
2)
    
{
  "11111111111111111111"
}
Returns: 400
A single instruction that accesses all 20 memory cells.
3)
    
{
  "1000",
  "1100",
  "1110"
}
Returns: 3
4)
    
{
  "111",
  "111",
  "110",
  "100"
}
Returns: 3

This is like the standard travelling salesman problem where where the current operation is stored in a form of bitmasked state and when the states is all1 it represents that all bits have been used. An important observation since here we dont need to remember what is the last operation done so instead of dp[last][bitmask] the state can be only dp[bitmask]. The other part is simple implementation.

Implementation
#include<bits/stdc++.h>#define pb push_backusing namespace std;int n;int take;int dp[(1<<20)+10];int p2[30];#define inf 10000000vector<int>nu;int call=0;   int solve(int pos,int bm,vector<int> nu,int pm){  //cout<<"bm "<<bm<<endl;       if(bm==take)   return 0;   if(dp[bm]!=-1)   return dp[bm];   int ret=inf;   for(int i=0;i<n;i++)   {              if((bm>>i &1 )==0)  { //    int newm=pm; //   cout<<"i "<<i<<" is not on "<<endl;     int num=0; //   cout<<"num gen check with nu[pos] "<<num<<" "<<nu[i]<<endl;     int addc=0; for(int cc=20;cc>=0;cc--) {       if((pm>>cc & 1)==0 && (nu[i]>>cc &1)==1)   addc++;     } // cout<<"addc "<<addc<<endl; call++; ret=min(ret,addc*addc+solve(i,bm|(1<<i),nu,pm|nu[i]));        }   }   dp[bm]=ret;   return ret;       }int minTime(vector<string>s){    vector<int> nu;    n=s.size();    take=(1<<n)-1;  //  cout<<"take "<<take<<endl;    memset(dp,-1,sizeof(dp));    for(int i=0;i<n;i++)    {          int num=0;          int l=s[i].size();          int x=l-1;          for(int k=l-1;k>=0;k--)          {                  num+=(s[i][k]-'0')<<(x-k);  } //  cout<<"num "<<num<<endl;  nu.pb(num); } int ret=solve(0,0,nu,0); return ret;}int main(){        int kk;        cin>>kk;        vector<string> v;        for(int i=0;i<kk;i++)        {           string s;           cin>>s;           v.pb(s); } int res=minTime(v); cout<<res<<endl; // cout<<call<<endl; return 0;}

No comments:

Post a Comment