锘??xml version="1.0" encoding="utf-8" standalone="yes"?> TSP闂錛堟梾琛屽晢闂錛夋槸鎸囨梾琛屽瑕佹梾琛宯涓煄甯傦紝瑕佹眰鍚勪釜鍩庡競緇忓巻涓斾粎緇忓巻涓嬈$劧鍚庡洖鍒板嚭鍙戝煄甯傦紝騫惰姹傛墍璧扮殑璺▼鏈鐭?/p> 鍋囪鐜板湪鏈夊洓涓煄甯傦紝0,1,2,3錛屼粬浠箣闂寸殑浠d環濡傚浘涓錛屽彲浠ュ瓨鎴愪簩緇磋〃鐨勫艦寮?br />
1.闂瀹氫箟
鍥句竴
鐜板湪瑕佷粠鍩庡競0鍑哄彂錛屾渶鍚庡張鍥炲埌0錛屾湡闂?錛?錛?閮藉繀欏誨茍涓斿彧鑳界粡榪囦竴嬈★紝浣夸唬浠鋒渶灝忋?/p>
璁緎, s1, s2, …, sp, s鏄粠s鍑哄彂鐨勪竴鏉¤礬寰勯暱搴︽渶鐭殑綆鍗曞洖璺紝鍋囪浠巗鍒頒笅涓涓煄甯俿1宸茬粡姹傚嚭錛屽垯闂杞寲涓烘眰浠巗1鍒皊鐨勬渶鐭礬寰勶紝鏄劇劧s1, s2, …, sp, s涓瀹氭瀯鎴愪竴鏉′粠s1鍒皊鐨勬渶鐭礬寰勶紝鎵浠SP闂鏄瀯鎴愭渶浼樺瓙緇撴瀯鎬ц川鐨勶紝鐢ㄥ姩鎬佽鍒掓潵姹傝В涔熸槸鍚堢悊鐨勩?/p>
鍋囪浠庨《鐐箂鍑哄彂錛屼護d(i, V’)琛ㄧず浠庨《鐐筰鍑哄彂緇忚繃V’(鏄竴涓偣鐨勯泦鍚?涓悇涓《鐐逛竴嬈′笖浠呬竴嬈★紝鏈鍚庡洖鍒板嚭鍙戠偣s鐨勬渶鐭礬寰勯暱搴︺?/p>
鎺ㄥ錛?鍒嗘儏鍐墊潵璁ㄨ)
①褰揤’涓虹┖闆嗭紝閭d箞d(i, V’)錛岃〃紺轟粠i涓嶇粡榪囦換浣曠偣灝卞洖鍒皊浜嗭紝濡備笂鍥劇殑 鍩庡競3->鍩庡競0(0涓鴻搗鐐瑰煄甯?銆傛鏃禿(i, V’)=Cis(灝辨槸 鍩庡競i 鍒?鍩庡競s 鐨勮窛紱?銆?/p>
②濡傛灉V’涓嶄負絀猴紝閭d箞灝辨槸瀵瑰瓙闂鐨勬渶浼樻眰瑙c備綘蹇呴』鍦╒’榪欎釜鍩庡競闆嗗悎涓紝灝濊瘯姣忎竴涓紝騫舵眰鍑烘渶浼樿В銆?/p>
d(i, V’)=min{Cik + d(k, V’-{k})}
娉細Cik琛ㄧず浣犻夋嫨鐨勫煄甯傚拰鍩庡競i鐨勮窛紱伙紝d(k, V’-{k})鏄竴涓瓙闂銆?/p>
緇間笂鎵榪幫紝TSP闂鐨勫姩鎬佽鍒掓柟紼嬪氨鍑烘潵浜嗭細
鐜板湪瀵歸棶棰樺畾涔変腑鐨勪緥瀛愭潵璇存槑TSP鐨勬眰瑙h繃紼嬨?鍋囪鍑哄彂鍩庡競鏄?0鍩庡競)
①鎴戜滑瑕佹眰鐨勬渶緇堢粨鏋滄槸d(0,{1,2,3}),瀹冭〃紺猴紝浠庡煄甯?寮濮嬶紝緇忚繃{1,2,3}涔嬩腑鐨勫煄甯傚茍涓斿彧鏈変竴嬈★紝姹傚嚭鏈鐭礬寰?
②d(0,{1,2,3})鏄笉鑳戒竴涓嬪瓙姹傚嚭鏉ョ殑錛岄偅涔堜粬鐨勫兼槸鎬庝箞寰楀嚭鐨勫憿錛熺湅涓婂浘鐨勭浜屽眰錛岀浜屽眰琛ㄦ槑浜哾(0,{1,2,3})鎵闇渚濊禆鐨勫箋傞偅涔堝緱鍑猴細
d(0,{1,2,3})=min {
C01+d(1,{2,3})
C02+d{2,{1,3}}
C03+d{3,{1,2}}
}
③d(1,{2,3})錛宒(2,{1,3})錛宒(3,{1,2})鍚屾牱涔熶笉鏄竴姝ュ氨鑳芥眰鍑烘潵鐨勶紝瀹冧滑鐨勮В涓鏍烽渶瑕佹湁渚濊禆錛屽氨姣斿璇磀(1,{2,3})
d(1,{2,3})=min{
C12+d(2,{3})
C13+d(3,{2})
}
d(2,{1,3})錛宒(3,{1,2})鍚屾牱闇瑕佽繖涔堟眰銆?/p>
④鎸夌収涓婇潰鐨勬濊礬錛屽彧鏈夋渶鍚庝竴灞傜殑錛屽綋褰揤’涓虹┖闆嗘椂錛孋is鐨勫兼墠鍙互姹傦紝瀹冪殑鍊兼槸鐩存帴浠?/p>
榪欏紶琛ㄩ噷姹傚緱鐨勩?/p>
灝哾(i, V’)杞崲鎴愪簩緇磋〃錛宒[i][j]
鍦ㄧ▼搴忎腑妯℃嫙濉〃鐨勮繃紼嬶紝涓昏瑕佽冭檻鍒癹榪欎釜鍙傛暟鐨勮〃紺猴紝瀹冭浠h〃涓涓泦鍚堬紝鍙互鐢ㄤ簩緇存暟緇勬潵琛ㄧず銆?/p>
娉細鐢變簬鏈漢姘村鉤鏈夐檺錛屽茍涓斾富瑕佸湪榪欓噷鏄綋鐜版濊礬錛屾墍浠ョ▼搴忓茍涓嶆槸寰堝畬鍠勶紝浠g爜璐ㄩ噺涔熶笉楂橈紝寰堝湴鏂瑰彲浠ュ啓寰楅氱敤涓浜涳紝鎵浠ヨ繖閲屽彧鏄彁渚涗竴涓弬鑰冿紝紼嬪簭鐨勮繘涓姝ュ畬鍠勶紝鐢辮鑰呰嚜鐢卞彂鎸ャ?/p>
#include
#include
int IsIncluded(int x,int array[3])//x鏄惁鍖呭惈鍦ㄦ暟緇勪腑
{
if((array[0] != x) && (array[1] != x) && (array[2] != x))
return 0;
return 1;
}
int Left(int k,int array[3],int V[8][3])//瀹炵幇V'-{k} 鐨勪笅鏍囨绱?nbsp;
{
int i = 0,index = 0,array_0_count = 0,array_1_count = 0,array_2_count = 0,array_3_count = 0;
int V_0_count = 0,V_1_count = 0,V_2_count = 0,V_3_count = 0;
int temp[3];
for(i = 0; i < 3; i++)
temp[i] = array[i];
for(i = 0; i < 3; i++)
if(temp[i] == k)
temp[i] = 0; //鐩稿綋浜庡幓鎺塳榪欎釜鍩庡競
for(i = 0; i < 3; i++)
{
if(temp[i] == 0)
array_0_count++;
else if(temp[i] == 1)
array_1_count++;
else if(temp[i] == 2)
array_2_count++;
else
array_3_count++;
}
for(index = 0; index < 8; index++)
{
for(i=0; i < 3; i++)
{
if(V[index][i] == 0)
V_0_count++;
else if(V[index][i] == 1)
V_1_count++;
else if(V[index][i] == 2)
V_2_count++;
else
V_3_count++;
}
if((array_0_count == V_0_count) && (array_1_count == V_1_count)
&& (array_2_count == V_2_count) && (array_3_count == V_3_count))
return index;
V_0_count = 0;
V_1_count = 0;
V_2_count = 0;
V_3_count = 0;
}
return 0;
}
void TSP(int d[4][8],int c[4][4],int V[8][3],int n)
{
int i = 0,j = 0,k = 0;
for(i = 1; i < n; i++)//V'涓虹┖鏃訛紝緇欒祴鍊鹼紝
d[i][0] = c[i][0];
for(j = 1; j < 7; j++)//鎸夊垪閬嶅巻涓嶅悓闆嗗悎錛寋1},{2},{3},{1,2},{1,3}.....
{
for(i = 1; i < n; i++)//閬嶅巻鍩庡競1錛?錛?
{
if( !IsIncluded(i,V[j]) )//i蹇呴』涓嶅湪闆嗗悎涓紝鍚﹀垯灝卞睘浜庣粡榪囦袱嬈★紝涓嶇鍚堥鎰?nbsp;
{
for(k = 0; k < 3; k++)//鍒嗗埆璇曟帰闆嗗悎涓殑姣忎竴鐐癸紝鍙栨渶灝忓?nbsp;
{
if((V[j][k] != 0) && ((c[i][V[j][k]] + d[V[j][k]][Left(V[j][k],V[j],V)]) < d[i][j]))
d[i][j] = c[i][V[j][k]] + d[V[j][k]][Left(V[j][k],V[j],V)];
}
}
}//end of for(i = 1; i < n; i++)//閬嶅巻鍩庡競1錛?錛?
}//end of for(j = 1; j < ((int)pow(2,n)-1); j++)
for(k = 0; k < 3; k++)//鍒嗗埆璇曟帰涓嬩竴姝ヤ負闆嗗悎涓殑浠諱綍涓鐐癸紝鍙栨渶灝忓?nbsp;
{
if((V[7][k] != 0) && (c[0][V[7][k]] + d[V[7][k]][Left(V[7][k],V[7],V)]) < d[0][7])
d[0][7] = c[0][V[7][k]] + d[V[7][k]][Left(V[7][k],V[7],V)];
}
}
void main()
{
int V[8][3]=
{
0,0,0,
0,0,1,
0,0,2,
0,0,3,
0,1,2,
0,1,3,
0,2,3,
1,2,3
};
int c[4][4]=
{
0,3,6,7,
5,0,2,3,
6,4,0,2,
3,7,5,0
};
int d[4][8]={0},i=0,j=0;
for(i=0; i<4; i++)
for(j=0; j<8; j++)
d[i][j]=1000; //鍋囪1000涓烘棤絀峰ぇ
TSP(d,c,V,4);
printf("The least road is:%d/n",d[0][7]);
}
2nd JOJ Cup Online VContest Problem
We all know that bunny is fond of carrots. One cloudy day, he was told that there would be a violenting flood coming soon to destroy the forests. He was scared and after not thinking too much he told himself that he had to escape. He suddenly recalled that there was a temple on the top of the hill and he might shelter there until the flood's past. But unfortunately there was no food for him on the top of the hill, so he had to take his carrots away along with himself. Then he moved to the foot of the hill and stopped. There was only one way for him to get the top of the hill, that is, a long staircase. Given the number of the steps of the staircase, he asked himself:"how many different ways of strides are there for him to get the top of the hill?". Of course, because of his height, he could only stride a limited range of steps. He was smart so much so that he got the answer quickly. Do you know how he did it?
Input Specification
The input consists of several test cases, each of which occupies a line containing M(1<=M<=40) and N(1<=N<=10), where M indicates the number of the steps of the staircase and N indicates the maximal number of steps the bunny can stride once.
Output Specification
Each test case should correspond to a line in the output. Your program should print an integer which is the answer.
Sample Input
4 2
5 4
Sample Output
5
15
棰樻剰鏄竴鍙厰瀛愯鍒拌窛紱諱負M錛堝崟浣嶄負1錛夌殑鍦版柟錛屽畠姣忔鏈澶氳蛋N錛岄棶鏈夊灝戠鏂規硶銆傝緭鍏 N 杈撳嚭r[m][n];
{
r[0][j]=1;
r[1][j]=1;
}
for(i=0;i<n;i++)
r[i][1]=1;
Input
Input will consist of a series of real numbers no greater than $50.00 each on a separate line. Each amount will be valid, that is will be a multiple of 5c. The file will be terminated by a line containing zero (0.00).
Output
Output will consist of a line for each of the amounts in the input, each line consisting of the amount of money (with two decimal places and right justified in a field of width 5), followed by the number of ways in which that amount may be made up, right justified in a field of width 12.
Sample input
0.20 2.00 0.00
Sample output
0.20 4 2.00 293
鍏稿瀷鐨勫姩鎬佽鍒掗棶棰橈細
鍙互鍏堣冭檻鍙湁n-1縐嶇‖甯侊紝V1,V2,V3,---Vn-1.璁懼噾鎴愭誨間負M鐨勬柟娉曟暟涓簉esult[M];
鐜板湪鍙堟坊鍔犱竴縐嶉潰鍊間負Vn鐨勭‖甯併?/pre>鍒欏挶浠渶瑕佷慨鏀逛竴浜涘鹼紝淇敼閭d簺澶т簬絳変簬Vn鐨剅esult[],涓嶅Θ璁綧澶т簬vn銆?/pre>鏂皉esult[M]=鍘焤esult[M]+result[M-Vn]+result[M-2*Vn]+----鐩村埌M-p*Vn<=0;錛?錛?/pre>瀹為檯涓婂鏋滃(2)閭f牱澶勭悊 浠庡ぇ浜庣瓑浜嶸n鐨剅esult[]寮濮嬪鐞嗙殑璇濓紝浠庡皬鍒板ぇ錛岃Vn=M-p*Vn;
鍒檙esult[M-p*Vn]絎竴涓渶鍏堟洿鏂幫紝result[M-p*Vn]+=result[M-p*Vn -Vn] 瑙?span style="COLOR: red">(2)
闅忓悗鏇存柊result[M-(p-1)*vn]錛屽垯鍙渶鐩存帴鍔犱笂result[M-p*Vn]鐨勫鹼紝鏃犻渶濡傦紙1錛夊紡閭f牱 绱姞銆?/pre>鐢ㄧ▼搴忚〃杈懼氨鏄?/pre>int coin[10]={5,10,20,50,100,200,500,1000,2000,5000};for(i=1;i<10;i++)for(j=coin[i];j<=50000;j+=5)result[j]+=result[j-Coin[i]]; (2) //
-------------榪樻湁涓涓敞鎰忕偣錛岃鐗涗漢鍗氬錛?a >http://hi.baidu.com/piaoshi111/blog/item/9a6de84a00a6e5f882025c89.html灝辨槸嫻偣鏁扮殑鍥涜垗浜斿叆錛?/pre>1.5錛屾誕鐐規暟鍙兘琛ㄤ負1.4999999錛屾墍浠ヤ箻浠?00鏃惰漿鍖栦負鏁村瀷鏄?49錛屽簲褰撴敞鎰忋?/pre>
]]>
You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutively 1 through V, where V is the number of vases, from left to right so that the vase 1 is the leftmost, and the vase V is the rightmost vase. The bunches are moveable and are uniquely identified by integers between 1 and F. These id-numbers have a significance: They determine the required order of appearance of the flower bunches in the row of vases so that the bunch i must be in a vase to the left of the vase containing bunch j whenever i < j. Suppose, for example, you have bunch of azaleas (id-number=1), a bunch of begonias (id-number=2) and a bunch of carnations (id-number=3). Now, all the bunches must be put into the vases keeping their id-numbers in order. The bunch of azaleas must be in a vase to the left of begonias, and the bunch of begonias must be in a vase to the left of carnations. If there are more vases than bunches of flowers then the excess will be left empty. A vase can hold only one bunch of flowers.
Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value, expressed by an integer. The aesthetic values are presented in a table as shown below. Leaving a vase empty has an aesthetic value of 0.
|
|
V A S E S |
||||
|
|
1 |
2 |
3 |
4 |
5 |
Bunches |
1 (azaleas) |
7 |
23 |
-5 |
-24 |
16 |
2 (begonias) |
5 |
21 |
-4 |
10 |
23 |
|
3 (carnations) |
-21 |
5 |
-4 |
-20 |
20 |
According to the table, azaleas, for example, would look great in vase 2, but they would look awful in vase 4.
To achieve the most pleasant effect you have to maximize the sum of aesthetic values for the arrangement while keeping the required ordering of the flowers.
ASSUMPTIONS
1 ≤ F ≤ 100 where F is the number of the bunches of flowers. The bunches are numbered 1 through F. F ≤ V ≤ 100 where V is the number of vases. -50 ≤ Aij ≤ 50 where Aij is the aesthetic value obtained by putting the flower bunch i into the vase j.
Input
The first line contains two numbers: F and V.
The following F lines: Each of these lines contains V integers, so that Aij is given as the j’th number on the (i+1)’st line of the input file.
Notice: The input contains several test cases.
Output
The output line will contain the sum of aesthetic values for your arrangement.
Sample Input
3 5
7 23 -5 -24 16
5 21 -4 10 23
-21 5 -4 -20 20
Sample Output
53
榪欓鍙互鐢ㄦ悳绱㈣繃錛屼絾鏄繕鍙互鐢╠p鎶宸э細璁劇疆涓涓暟緇刟[i]
瀛樻斁鎵鏈夐暱搴︿負i鐨勪笂鍗囧瓙搴忓垪涓渶灝忕殑鏈厓绱犲鹼紝姣斿璇村彧鏈変袱涓暱搴︿負3鐨勪笂鍗囧瓙搴忓垪123鍜?24錛岄偅涔坅[3]涓瓨鏀劇殑灝辨槸3錛堟湯鍏冪礌3<4錛?/p>
閭d箞褰撴潵涓涓柊鏁癲ata鏃訛紝濡傛灉瀹冪殑鍊煎ぇ浜庢渶闀塊暱搴︾殑鏈厓绱犵殑鍊鹼紙鍗砤[ans]錛夛紝鍒檃ns++錛涗笖a[ans]=data;
鍚﹀垯錛岄氳繃浜屽垎鏌ユ壘錛堟暟緇刟涓殑鍏冪礌涓洪掑錛夛紝灝嗘渶鎺ヨ繎data涓斿ぇ浜巇ata鐨勯偅涓厓绱犳洿鏂頒負data錛屾棦鏈灝忕殑澶т簬瀹冪殑鏁般?br>渚嬪1,5,3,4,涔嬪悗鏉ヤ釜2錛宎[1]=1,a[2]=3,a[3]=4;鍒欐洿鏂癮[2]=2;
鐢變簬浜屽垎鏌ユ壘澶嶆潅搴︿負log(n)錛屽鍥翠負n,鎬葷殑澶嶆潅搴︿負nlogn
棰樼洰澶ф剰:
鍘熸枃鍦板潃錛?a >http://hi.baidu.com/flymouse/blog/item/fd1378f05c7ff7c37931aac3.html璇誨叆涓涓猲*n鐨勬暟緇?姣斿 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2 浠庨噷闈換鎰忔埅鍙栦竴涓煩闃?浣垮緱鐭╅樀鎵鍖呭惈鐨勬暟瀛楃殑鍜屾渶澶? 鎴彇鍑烘潵鐨勭煩闃?鍜屼負15 9 2 -4 1 -1 8 --------------------------------------------------------- POJ 1050 鎴戠殑瑙i鎶ュ憡錛?br> 榪欎釜棰樼洰寰堢粡鍏哥殑璇達紝O錛圢^3錛夌殑DP銆?br> 棣栧厛鍋朵滑鑰冨療榪欐牱鐨勯鐩紝綆鍖栫増錛?br> 宸茬煡涓鍒楁暟錛屾眰浠繪剰榪炵畫鑻ュ共涓暟鍜岀殑鏈澶у箋?br> SAMPLE錛?nbsp;3 2 -6 2 -1 7 鍘熸暟3 2 -6 2 -1 7 澶勭悊3 5 -1 2 1 8 鍥犱負鏄繛緇嫢騫蹭釜鑷劧鏁扮殑鍜岋紝閭d箞錛屽墠闈㈢殑鏌愪釜鏁板瓧鍙栦笌涓嶅彇鐨勬潯浠跺湪浜庯細浠ュ墠闈㈣繖涓暟瀛椾負緇撳熬鐨勮繛緇暟鐨勫拰鏈澶у兼槸鍚﹀ぇ浜?錛屽鏋滃ぇ浜?錛岄偅涔堣繖涓暟瀛楀繀鐒惰浼氬嚭鐜板湪鍖呮嫭鏁板瓧鐨勫簭鍒椾腑錛屽惁鍒欐棤娉曞仛鍒版渶澶с?br> 鎵浠ワ紝鏄劇劧銆傚鐞嗙殑鍘熷垯鏄痬axn[i]=max{0,maxn[i-1]}+a[i]; 鐢變簬鏃犻』璁板綍浣嶇疆銆傛墍浠ワ紝鍙互鐩存帴鐢ㄤ竴涓彉閲弒um浠f浛maxn鏁扮粍銆侽(n)鐨勬壂鎻忓嵆鍙?br> 鍗曞垪鏁板瓧鐨勯棶棰樿В鍐充簡錛屼笅闈㈡垜浠冨療澶氬垪鏁板瓧鐨?br> sample: 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2 鎴戜滑鍙互灝嗗鍒楁暟瀛楄漿鎹㈡垚鍗曞垪鏁板瓧鏉ュ仛錛?nbsp;鍙互榪欐牱璁炬兂錛岀粨鏋滄槸涓涓暱鏂瑰艦錛屾垜浠妸浠栧帇鎵侊紝浣垮緱瀹戒負1銆?br> 寮曞叆杈呭姪鏁扮粍st,st[i][j]浠h〃絎琲鍒椾粠絎?琛屽紑濮嬬殑鏁板瓧绱姞鍒扮j琛岀殑鍊箋傞偅涔堬紝鎴戜滑姣忔鍘嬫墎鐨勬椂鍊欙紝灝卞彲浠ョ敤st[i][j]-st[i][k-1]鏉ヨ〃紺虹i鍒椾粠絎琸涓暟瀛楃瘡鍔犲埌絎琷涓暟瀛楃殑鍊箋傝揪鍒板帇緙╃殑鏁堟灉銆傜劧鍚庣敤涓婇潰鍗曞垪鏁板瓧鐨勬柟娉曟潵鍋氥傜畻娉曟椂闂村鏉傚害O (N^3) Source Problem Id:1050 User Id:galaxy Memory:112K Time:0MS Language:G++ Result:Accepted /* Name:POJ 1050 Copyright: flymouse@galaxy Author:chenlei Date: 15-02-06 07:36 Description: DP O(N^3) */ #include <stdio.h> #include <string.h> #define mt 101 int main() { int a[mt][mt]; int st[mt][mt]; int p,k,n,i,j,sum,maxn; //freopen("in.txt","r",stdin); scanf("%d",&n); for (i=1;i<=n;i++) for (j=1;j<=n;j++) scanf("%d",&a[i][j]); memset(st,0,sizeof(st)); for (i=1;i<=n;i++) for (j=1;j<=n;j++) st[i][j]=st[i][j-1]+a[j][i]; maxn=0; for (i=1;i<=n;i++) { for (j=i;j<=n;j++) { p=st[1][j]-st[1][i-1]; sum=p; for (k=2;k<=n;k++) { if (sum>0) sum+=st[k][j]-st[k][i-1]; else sum=st[k][j]-st[k][i-1]; if (sum>p) p=sum; } if (p>maxn) maxn=p; } } printf("%d\n",maxn); return 0; |
Status | In/Out | TIME Limit | MEMORY Limit | Submit Times | Solved Users | JUDGE TYPE |
---|---|---|---|---|---|---|
![]() |
stdin/stdout | 3s | 10240K | 717 | 196 | Standard |
Mr. Jojer is a very famous chemist. He is doing a research about behavior of a group of atoms. Atoms may have different energy and energy can be positive or negative or zero, e.g. 18 or -9. Absolute value of energy can not be more than 100. Any number of continuous atoms can form an atom-group. Energy of an atom-group is defined by the sum of energy of all the atoms in the group. All the atoms form an atom-community which is a line formed by all the atoms one by one. Energy of an atom-community is defined by the greatest energy of an atom-group that can be formed by atoms in the atom-community. The problem is, given an atom-community, to calculate its energy.
The input contains several test cases. Each test case consists of two lines describing an atom-community. The first line of each test case contains an integer N(N<=1000000), the number of atoms in the atom-community. The second line of each test case contains N integers, separated by spaces, each representing energy of an atom, given in the order according to the atom-community. The last test case marks by N=-1, which you should not proceed.
For each test case(atom-community description), print a single line containing the energy.
5 8 0 6 4 -1 -1
18
鐞嗚В棰樻剰寰堥噸瑕侊紝棰樼洰鐨勬剰鎬濇槸璇?鍦╩涓腑閫?nbsp; 榪炵畫鐨刵涓猘tom鑳介噺鍊?浣垮叾鏈澶с?br>紼嬪簭涓璼umtemp錛宻um銆俿umtemp紜畾鐨勬槸鍏跺乏杈圭晫錛宻um紜畾鍏跺彸杈圭晫銆?br>sumtemp紜畾宸﹁竟鍓峮涓暟涔嬪拰涓鴻礋鐨勬渶澶х殑n錛屼笖絎琻涓暟鏄劇劧涓鴻礋錛岀劧鍚庝粠n+1寮濮嬮夋暟銆?br>sum紜畾浜嗗彸杈圭晫浣垮叾鏈澶с?br>姹傚拰鏈澶ч兘鍙互鐢ㄨ繖縐嶆濊礬錛侊紒錛侊紒錛侊紒錛侊紒錛侊紒
涓句緥
1錛?錛?4錛?錛?錛?2錛?錛?錛?6錛?錛?3錛?錛?0
璇風湅sumt1=1,sum=1
sumt2=2;sum=1+2=3;
sum3=sum2-4=-1;鍒檚umtemp=0;sum涓嶅彉銆?br>sum4=sumtemp+4;sum<sum4,sum=4;
sum5=sumtemp+2錛泂um<sum5,sum=6
鎬諱箣sum鍙湁鍦╯um<sumtemp鏃舵墠淇敼銆俿umtemp<0鍒欐竻0.
涓緇磀p.
#include"stdio.h"
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
int n;
int a;
while(scanf("%ld",&n),n!=-1)
{
long long sum=-0x7fffffff,sumtemp=-0x7fffffff;
for(long i=0;i<n;i++)
{
scanf("%d",&a);
if(sumtemp>0)
sumtemp+=a;
else
sumtemp=a;
if(sumtemp>sum)
sum=sumtemp;
}
printf("%lld\n",sum);
}
return 0;
}
Status | In/Out | TIME Limit | MEMORY Limit | Submit Times | Solved Users | JUDGE TYPE |
---|---|---|---|---|---|---|
![]() |
stdin/stdout | 15s | 8192K | 479 | 114 | Standard |
The first line of input contains n the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight of person 2; and so on. Each weight is an integer between 1 and 450. There are at most 100 people at the picnic.
The input may contain several test cases.
Your output will be a single line containing 2 numbers: the total weight of the people on one team, and the total weight of the people on the other team. If these numbers differ, give the lesser first.
3 100 90 200
190 200
鍒╃敤dp鎬濇兂 錛宯涓哄伓鏁版椂姹傚嚭s(n,n/2)錛宯涓哄鏁版椂 涔熸槸s(2n,n/2)錛屽拰sum/2鏈鎺ヨ繎鐨勯偅涓傞潪甯哥粡鍏哥殑鎬濊礬銆?br>S(k, 1) = {A[i] | 1<= i <= k}
S(k, k) = {A[1]+A[2]+…+A[k]}
S(k, i) = S(k-1, i) U {A[k] + x | x灞炰簬S(k-1, i-1) }
//涓涓嬩唬鐮佸彧鑳界敤浜巗um鐗瑰埆灝忕殑鎯呭喌錛屽惁鍒欎細瓚呮椂錛侊紒錛侊紒錛侊紒錛侊紒錛侊紒錛?br>#include<iostream>
#include<cstdlib>
#define MAX 101
#define min(a,b) ((a)<(b) ? (a) : (b))
using namespace std;
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
int num;
int a[MAX],i,j,k,l,m,NUM;
bool s[MAX][2500];
while(cin>>num)
{
int sum=0;
for(i=1;i<=num;i++)
{
cin>>a[i];
sum+=a[i];
}
if(num%2==0)NUM=num/2;
else NUM=num/2+1;
for(i=0;i<=num;i++)
for(j=0;j<=sum/2;j++)
s[i][j]=false;//琛ㄧず鍙杋涓墿鍝佽兘鍚﹁揪鍒伴噸閲忔槸j.
s[0][0]=true;
for(k=1;k<=num;k++)//蹇呴』鍦ㄦ渶澶栧眰錛屽厓绱犱笉鑳介噸澶?
for(j=min(k,NUM);j>=1; j--)//閫掑噺鐨勭粨鏋滄槸浣垮緱涓嶄細鍑虹幇鍦ㄥ悓涓灞傛鐨勪簰涓哄洜鏋?銆併併併併併併併併併併佸閥濡欑殑瀹炵幇浜嗚鏈笂鐨勫簭鍋跺鐢熸垚娉曘?br> for(i=a[k];i<=sum/2;i++)
{
if(s[j-1][i-a[k]])
s[j][i]=true;
}
for(i=sum/2; i>=0; i--) {
if(s[NUM][i]) {
cout <<i<<" "<<sum-i<<endl;
break;
}
}
}
//system("PAUSE");
return 0;
}
涓嬩竴嬈″疄鐜頒竴涓簭鍋剁敓鎴愭硶銆?/p>
#include <iostream>
#include <functional>
using namespace std;
int a[101];
bool b[101][45002];
int main(){
// freopen("s.txt","r",stdin);
// freopen("key.txt","w",stdout);
int N,M,i,j,k;
while(scanf("%d",&N)!=EOF){
memset(b,0,sizeof(b));
a[0]=M=0;
for(i=1;i<=N;i++){
scanf("%d",a+i);
M+=a[i];
}
b[0][0]=1;
for(k=1;k<=N;k++){
for(i=1;i<=N/2;i++){
for(j=M/2;j>=0;j--){
if(b[i-1][j]){
b[i][j+a[k]]=1;
}
}
}
}
for(i=M/2,j=M/2+1;i>=0;i--,j++){
if(b[N/2][i]){
printf("%d %d\n",i,M-i);
break;
}
if(b[N/2][j]){
printf("%d %d\n",M-j,j);
break;
}
}
}
return 0;
}
3 1 2 9
15
One curious child has a set of N little bricks. From these bricks he builds different staircases. Staircase consists of steps of different sizes in a strictly descending order. It is not allowed for staircase to have steps equal sizes. Every staircase consists of at least two steps and each step contains at least one brick. Picture gives examples of staircase for N=11 and N=5:
Your task is to write a program that reads from input numbers N and writes to output numbers Q - amount of different staircases that can be built from exactly N bricks.
3 5 0
1 2鏂規硶1錛屽姩鎬佽鍒?br>