Description-Problem Description
The son of XC, the favorite game of Xiao XC, uses beautiful blocks to build a castle. The castle is built with blocks of cubes. Each layer of the castle is a block. Xiao XC is a child smarter than his father XC. When he finds the base Castle, if the building blocks below are larger than the ones above, the castle will not be easy to fall down. So he always follows this rule when he is at base Castle.
Xiao XC wants to give his castle to beautiful girls in the kindergarten, which can increase his goodwill (obviously increasing the number of harem students)Σ (° △° \ ||\|)). To be fair, he decided to give the castle as tall as every girl, so as to avoid disputes between female children in order to get a more beautiful castle. But he found that he did not consider this in advance when he was at base Castle. So now he wants to transform the castle. As he had no extra building blocks, he had the opportunity to come up with a clever transformation plan. He decided to move some blocks from each castle, so that each castle would eventually be as tall. To make his castle more magnificent, he felt that the final castle should be as high as possible.
Task:
Please help Xiao XC compile a program and decide which building blocks should be moved based on the information of all the castles in his base to achieve the best results.
(Short review: a clever little tease)
Input-input data
The first line is an integer n (n <= 100), indicating a total of several castles. Each line in the N rows below is a series of non-negative integers separated by a space, and the edges of all blocks in a castle are given in sequence from bottom to top. End with-1. The number of blocks in a castle cannot exceed 100, and the length of each block cannot exceed 100.
Output-output data
An integer that represents the maximum possible height of the final Castle. If no suitable solution is found, 0 is output.
Input/Output example-input/output sample input
22 1 -13 2 1 -1
Output
3
It is said that this question was originally selected in Zhejiang Province,Are you sure you're not kidding me !!!
This is actually a simple backpack problem. The time is a little tight, Ms can pass
(Beg you to give the algorithms below 50 ms! God knows orz !!)
The height of each castle is used to obtain the possible height after the building blocks are taken, and the same height may be enumerated from the top down.
Ugly code
#include "cstdio"int n,g[101][101],h[101],i,j,k,hs[101];int ht;bool f[101][10001],flag;void read(){scanf("%d",&n);for(i=1;i<=n;++i){j=1;while(scanf("%d",&k),k!=-1){h[i]+=(g[i][j]=k);++j;}f[i][0]=true;hs[i]=--j;}}int main(int argc, char const *argv[]){read();for(i=1;i<=n;++i){for(j=1;j<=hs[i];++j){for(k=h[i];k>-1;--k){f[i][k]=f[i][k]||f[i][k-g[i][j]];//if this height is reachable after removing some of the blocks}}}ht=20000;//of course, a number that is much bigger than the max height has been treated as INFfor(i=1;i<=n;++i){if(h[i]
It seems very short?
The comment and other redundant code must be removed.
#include "cstdio"int n,g[101][101],h[101],i,j,k,hs[101];int ht;bool f[101][10001],flag;void read(){scanf("%d",&n);for(i=1;i<=n;++i){j=1;while(scanf("%d",&k),k!=-1){h[i]+=(g[i][j]=k);++j;}f[i][0]=true;hs[i]=--j;}}int main(){read();for(i=1;i<=n;++i)for(j=1;j<=hs[i];++j)for(k=h[i];k>-1;--k)f[i][k]=f[i][k]||f[i][k-g[i][j]];ht=20000;for(i=1;i<=n;++i)if(h[i]