Esempio n. 1
0
 vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
     vector<vector<int>> dp1;
     vector<vector<int>> dp2;
     getDP(nums1, dp1);
     getDP(nums2, dp2);
     reverse(dp1.begin(), dp1.end());
     reverse(dp2.begin(), dp2.end());
     vector<int> ans;
     bool isFirst = true;
     for (int m = 0; m <= k && m < dp1.size(); m++) {
         int n = k - m;
         if (n < 0) continue;
         if (n >= dp2.size()) continue;
         vector<int> myans = merge(dp1[m], dp2[n]);
         if (isFirst) {
             isFirst = false;
             ans = myans;
         } else {
             if (isLarger(myans, 0, ans, 0)) {
                 ans = myans;
             }
         }
     }
     return ans;
 }
int getDP(int i){
    if(DP[i] != -1) return DP[i];
    else{
         if(i == 0) return 1;
         long Tmp = getDP((int)i - sqrt(i)) + getDP((int)log(i)) + getDP((int)(i * sin(i) * sin(i)));
         DP[i] = Tmp % 1000000;
         return DP[i];
         }
    }
Esempio n. 3
0
 int getDP(int** dp, int i, int j) {
     if (i >= j) return 0;
     if (dp[i][j] != -1) return dp[i][j];
     int minCost = INT_MAX;
     for (int k = i; k <= j; k++) {
         minCost = min(minCost, k + max(getDP(dp, i, k-1), getDP(dp, k+1, j)));
     }
     dp[i][j] = minCost;
     return minCost;
 }
Esempio n. 4
0
 int getMoneyAmount(int n) {
     int** dp;
     dp = new int*[n+4];
     for (int i = 0; i <= n; i++) {
         dp[i] = new int[n+4];
         for (int j = 0; j <= n; j++) dp[i][j] = -1;
     }
     for (int i = 1; i <= n; i++) {
         for (int j = i - 1; j >= 1; j--) {
             getDP(dp, j, i);
         }
     }
     return getDP(dp, 1, n);
 }
int main(){
     for(int i = 0; i <= 1000000; i++) DP[i] = -1;
    while(scanf("%d",&i) && i != -1){
            printf("%d\n", getDP(i));             
            }
    return 0;
    }
int getDP(int c1,int c2,int c3,int c4){
	if(basket[c1][c2][c3][c4] != -1)	return basket[c1][c2][c3][c4];
	basket[c1][c2][c3][c4] = MAX;
	if(c1>0&&basket[c1][c2][c3][c4]==MAX){
		int b = putIntoBasket(getDP(c1-1,c2,c3,c4),piles[0][c1]);
		if(basket[c1][c2][c3][c4] > b)	basket[c1][c2][c3][c4] = b;
	}
	if(c2>0&&basket[c1][c2][c3][c4]==MAX){
		int b = putIntoBasket(getDP(c1,c2-1,c3,c4),piles[1][c2]);
		if(basket[c1][c2][c3][c4] > b)	basket[c1][c2][c3][c4] = b;
	}
	if(c3>0&&basket[c1][c2][c3][c4]==MAX){
		int b = putIntoBasket(getDP(c1,c2,c3-1,c4),piles[2][c3]);
		if(basket[c1][c2][c3][c4] > b)	basket[c1][c2][c3][c4] = b;
	}
	if(c4>0&&basket[c1][c2][c3][c4]==MAX){
		int b = putIntoBasket(getDP(c1,c2,c3,c4-1),piles[3][c4]);
		if(basket[c1][c2][c3][c4] > b)	basket[c1][c2][c3][c4] = b;
	}
	return basket[c1][c2][c3][c4];
}
Esempio n. 7
0
/*----------------------------------------------------*/
void display(void) 
{ 
   int   i, j; 
   float x1, x2, y, px, py;

   sa = 0.010*cos(iframe*2.0*M_PI/200.0);
   for (i = 0; i < NMESH-1; i++) {
      x1 = DM*i; x2 = x1 + DM;
      glBegin(GL_QUAD_STRIP);
      for (j = 0; j < NMESH; j++) {
          y = DM*j;
          glTexCoord2f(x1, y); 
          getDP(x1, y, &px, &py); 
          glVertex2f(px, py);

          glTexCoord2f(x2, y); 
          getDP(x2, y, &px, &py); 
          glVertex2f(px, py);
      }
      glEnd();
   }
   iframe = iframe + 1;

   glEnable(GL_BLEND); 
   glCallList(iframe % Npat + 1);
   glBegin(GL_QUAD_STRIP);
      glTexCoord2f(0.0,  0.0);  glVertex2f(0.0, 0.0);
      glTexCoord2f(0.0,  tmax); glVertex2f(0.0, 1.0);
      glTexCoord2f(tmax, 0.0);  glVertex2f(1.0, 0.0);
      glTexCoord2f(tmax, tmax); glVertex2f(1.0, 1.0);
   glEnd();
   glDisable(GL_BLEND);
   glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
                    0, 0, NPIX, NPIX, 0);
   glutSwapBuffers(); 
}
Esempio n. 8
0
character* golem::attack(character* enemy,const short& plusDP) throw(Error)
try
{//metodo ereditato
    short hp;
    if(enemy->getHP() && isAlive())
    {
        if(enemy!=this)//se è un attacco offensivo non è possibile autoinfliggerselo
            hp=enemy->getHP()-(getDP()+plusDP+getStrength());
        else//enemy==this;non esistono attacchi curativi per i tipi demon
        {	
            throw Error();
            return NULL;
        }
        enemy->setHP(hp);
    }
    return enemy;
}
catch(Error e){
    cout<<"Invalid target "<<endl;
}
int main(){
//	freopen("in.txt","r",stdin);
	while(scanf("%d",&n)&&n>0){
		for(int i=1;i<=n;++i){
			scanf("%d%d%d%d",&piles[0][i],&piles[1][i],&piles[2][i],&piles[3][i]);
		}
		for(int i=0;i<=n;++i){
			for(int j=0;j<=n;++j){
				for(int k=0;k<=n;++k){
					for(int t=0;t<=n;++t){
						basket[i][j][k][t] = -1;
					}
				}
			}
		}
		basket[1][0][0][0] = putIntoBasket(0,piles[0][1]);
		basket[0][1][0][0] = putIntoBasket(0,piles[1][1]);
		basket[0][0][1][0] = putIntoBasket(0,piles[2][1]);
		basket[0][0][0][1] = putIntoBasket(0,piles[3][1]);
	
		int max = 0;
		for(int i=0;i<=n;++i){
			for(int j=0;j<=n;++j){
				for(int k=0;k<=n;++k){
					for(int t=0;t<=n;++t){
//						printf("%d %d %d %d %d\n",i,j,k,t,getDP(i,j,k,t));
						int get = (i+j+k+t-countBasket(getDP(i,j,k,t)))/2;
						if(max < get){
							max = get;
//							printf("MAX:\n");
//							printBasket(getDP(i,j,k,t));
//							printf("%d %d %d %d %d\n",i,j,k,t,countBasket(getDP(i,j,k,t)));
						}
					}
				}
			}
		}
		printf("%d\n",max);
	}
	return 0;
}
int main() {
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--) {
        initialize();
        int res = MAX;
        for(int i=0; i<n-1; ++i) {
            for(int j=0; j<n-1; ++j) {
                if(res > getDP(n-1,i,j))	res = getDP(n-1,i,j);
                if(res > getDP(i,n-1,j))	res = getDP(i,n-1,j);
                if(res > getDP(i,j,n-1))	res = getDP(i,j,n-1);
            }
        }
        printf("%d\n",res);
    }
}
int getDP(int a,int b,int c) {
    if(dp[a][b][c] >= 0)	return dp[a][b][c];
    int min = MAX;
    int m = ((a>b)?a:b)>c?((a>b)?a:b):c;
    if(m==a) {
        if(b<m-1&&c<m-1) {
            if(min>getDP(a-1,b,c)+dist[a-1][a])	min = getDP(a-1,b,c)+dist[a-1][a];
        }
        else {
            for(int i=0; i<a; ++i) {
                if(min > getDP(i,b,c)+dist[i][a])
                    min = getDP(i,b,c)+dist[i][a];
            }
        }
    }
    if(m==b) {
        if(a<m-1&&c<m-1) {
            if(min>getDP(a,b-1,c)+dist[b-1][b])	min = getDP(a,b-1,c)+dist[b-1][b];
        }
        else {
            for(int i=0; i<b; ++i) {
                if(min > getDP(a,i,c)+dist[i][b])
                    min = getDP(a,i,c)+dist[i][b];
            }
        }
    }
    if(m==c) {
        if(a<m-1&&b<m-1) {
            if(min>getDP(a,b,c-1)+dist[c-1][c])	min = getDP(a,b,c-1)+dist[c-1][c];
        }
        else {
            for(int i=0; i<c; ++i) {
                if(min > getDP(a,b,i)+dist[i][c])
                    min = getDP(a,b,i)+dist[i][c];
            }
        }
    }

    dp[a][b][c] = min;
    return min;
}