题目描述
给出一段序列,选出其中连续且非空的一段使得这段和最大。
输入输出格式
输入格式:
第一行是一个正整数NN,表示了序列的长度。
第二行包含NN个绝对值不大于1000010000的整数A_iAi,描述了这段序列。
输出格式:
一个整数,为最大的子段和是多少。子段的最小长度为11。
输入输出样例
说明
【样例说明】
2,-4,3,-1,2,-4,3中,最大的子段和为4,该子段为3,-1,2.
【数据规模与约定】
对于40%的数据,有N ≤ 2000。
对于100%的数据,有N ≤ 200000。
emmmmm,不能用dp[i] = max(dp[i-1]+a[i],dp[i]) ,因为有一个用例不能通过
该输入用例:
2000 -145 -1033 -407 -656 -1063 -231 -302 -531 -871 -366 -987 -649 -172 -527 -788 -330 -876 -181 -583 -668 -176 -453 -981 -284 -115 -548 -310 -753 -134 -921 -128 -1085 -414 -1009 -143 -913 -1042 -829 -315 -1076 -285 -193 -190 -1022 -698 -387 -572 -931 -775 -422 -460 -193 -480 -730 -808 -781 -383 -989 -240 -994 -212 -219 -795 -1058 -551 -555 -808 -592 -292 -290 -280 -407 -951 -224 -214 -223 -199 -273 -956 -279 -978 -410 -920 -241 -602 -834 -847 -717 -343 -311 -868 -441 -992 -476 -242 -682 -760 -756 -921 -998 -727 -574 -248 -758 -993 -231 -352 -892 -974 -453 -259 -113 -417 -697 -618 -679 -117 -416 -972 -976 -1038 -402 -362 -515 -272 -464 -1073 -498 -435 -711 -181 -921 -1098 -360 -263 -364 -989 -113 -321 -148 -648 -1041 -669 -487 -1051 -282 -952 -1088 -836 -830 -377 -918 -134 -703 -1042 -259 -730 -165 -577 -634 -909 -675 -1059 -280 -570 -292 -1094 -389 -1004 -264 -154 -931 -203 -1066 -553 -127 -814 -492 -747 -884 -743 -104 -544 -426 -866 -245 -264 -511 -449 -772 -537 -739 -409 -298 -250 -638 -118 -418 -672 -978 -186 -541 -128 -380 -236 -206 -391 -432 -483 -448 -1085 -417 -521 -412 -602 -555 -1074 -393 -1054 -809 -666 -352 -1035 -775 -820 -940 -1032 -255 -822 -158 -191 -271 -125 -304 -486 -343 -721 -148 -124 -194 -942 -892 -170 -792 -613 -837 -776 -1060 -352 -724 -981 -967 -822 -626 -536 -255 -226 -746 -474 -526 -162 -625 -566 -564 -1054 -864 -361 -635 -597 -650 -701 -822 -612 -470 -316 -868 -446 -1008 -693 -942 -873 -205 -229 -692 -1014 -473 -516 -398 -882 -167 -727 -507 -183 -824 -826 -1067 -246 -195 -740 -964 -904 -570 -887 -1025 -499 -630 -877 -896 -297 -1049 -122 -589 -218 -1099 -677 -986 -363 -347 -669 -740 -804 -534 -513 -448 -568 -138 -611 -900 -863 -440 -984 -719 -482 -241 -897 -779 -393 -112 -463 -997 -368 -570 -346 -514 -790 -556 -208 -828 -691 -282 -153 -539 -164 -504 -897 -119 -624 -422 -264 -185 -744 -882 -349 -490 -595 -599 -846 -830 -889 -984 -435 -634 -228 -587 -1025 -338 -382 -670 -780 -857 -533 -268 -802 -1034 -302 -149 -123 -867 -627 -448 -354 -756 -752 -109 -993 -136 -975 -367 -168 -196 -840 -237 -863 -209 -255 -698 -365 -480 -386 -680 -216 -512 -249 -525 -648 -184 -781 -121 -1099 -975 -1045 -793 -159 -972 -796 -491 -220 -296 -394 -624 -307 -837 -423 -1008 -296 -676 -744 -328 -422 -232 -142 -506 -205 -379 -664 -402 -158 -985 -726 -834 -1078 -770 -276 -689 -226 -622 -306 -550 -262 -1086 -870 -540 -469 -191 -957 -384 -110 -621 -446 -417 -270 -743 -695 -175 -487 -414 -809 -876 -186 -1023 -235 -734 -605 -434 -980 -541 -609 -813 -271 -257 -993 -949 -759 -672 -281 -1080 -354 -322 -634 -797 -319 -1006 -890 -415 -853 -902 -924 -819 -649 -416 -957 -103 -148 -879 -1037 -255 -537 -697 -139 -601 -140 -1045 -858 -913 -135 -991 -1024 -100 -965 -459 -679 -168 -481 -932 -1007 -1085 -984 -293 -909 -146 -109 -724 -618 -164 -773 -126 -1044 -288 -761 -984 -120 -1092 -633 -322 -946 -973 -1025 -1013 -1060 -336 -161 -322 -327 -778 -925 -879 -1052 -1077 -382 -151 -820 -916 -357 -679 -916 -310 -613 -345 -696 -981 -102 -952 -128 -418 -140 -744 -929 -1040 -848 -495 -553 -983 -787 -402 -1006 -225 -942 -887 -979 -300 -273 -713 -441 -363 -844 -950 -790 -818 -481 -202 -511 -807 -430 -233 -106 -711 -130 -390 -328 -765 -608 -605 -171 -1040 -234 -683 -450 -555 -506 -582 -148 -212 -350 -762 -303 -1043 -163 -443 -628 -663 -603 -375 -100 -180 -450 -636 -968 -493 -994 -510 -233 -388 -728 -311 -192 -559 -806 -721 -1085 -942 -951 -172 -212 -1042 -837 -499 -638 -129 -452 -625 -582 -848 -131 -656 -563 -594 -1081 -892 -1064 -581 -866 -213 -890 -294 -284 -121 -1084 -452 -830 -351 -431 -719 -775 -212 -629 -753 -228 -466 -329 -405 -901 -165 -436 -672 -485 -391 -729 -152 -500 -990 -849 -758 -196 -450 -589 -713 -611 -630 -962 -1019 -326 -443 -267 -378 -772 -663 -548 -969 -554 -1050 -280 -397 -111 -305 -486 -791 -119 -484 -970 -163 -456 -585 -105 -339 -902 -627 -719 -870 -401 -710 -456 -1048 -672 -413 -111 -1056 -903 -652 -603 -948 -340 -912 -1078 -933 -1055 -728 -184 -534 -1009 -274 -905 -180 -619 -1014 -239 -986 -1017 -419 -977 -886 -418 -458 -533 -869 -520 -672 -760 -647 -904 -690 -1010 -664 -197 -392 -308 -121 -207 -204 -818 -665 -836 -746 -883 -598 -850 -593 -518 -655 -355 -434 -572 -997 -532 -681 -640 -677 -595 -327 -514 -137 -122 -611 -283 -437 -982 -1070 -734 -1092 -192 -1067 -855 -197 -637 -419 -347 -359 -146 -231 -214 -975 -644 -787 -463 -526 -605 -664 -357 -574 -939 -493 -909 -388 -647 -120 -823 -190 -207 -335 -977 -378 -1029 -703 -560 -990 -773 -627 -657 -286 -863 -1070 -829 -528 -240 -265 -526 -352 -692 -357 -847 -629 -763 -389 -376 -1026 -940 -340 -1008 -404 -539 -200 -143 -185 -855 -243 -196 -1093 -515 -320 -812 -506 -873 -155 -539 -333 -166 -1037 -414 -914 -439 -1066 -637 -675 -780 -1078 -168 -656 -201 -1031 -808 -347 -941 -217 -899 -909 -564 -630 -402 -379 -328 -287 -942 -305 -492 -1038 -373 -804 -637 -441 -203 -369 -481 -925 -422 -581 -566 -507 -851 -150 -715 -414 -782 -579 -402 -195 -1078 -659 -217 -303 -803 -812 -410 -155 -318 -110 -309 -166 -1006 -714 -662 -647 -653 -1042 -854 -235 -186 -989 -550 -852 -907 -227 -1080 -733 -294 -106 -206 -204 -152 -610 -206 -612 -156 -944 -642 -164 -278 -892 -795 -124 -705 -1038 -609 -990 -861 -268 -1078 -782 -257 -984 -457 -144 -873 -663 -188 -821 -529 -345 -911 -149 -564 -997 -733 -1074 -429 -550 -117 -296 -613 -470 -783 -294 -975 -193 -107 -615 -216 -161 -585 -1046 -832 -604 -919 -198 -205 -159 -408 -526 -201 -127 -427 -1078 -996 -822 -593 -1046 -195 -552 -145 -307 -1055 -1025 -1099 -170 -913 -962 -742 -343 -947 -107 -183 -440 -113 -623 -1066 -136 -318 -591 -847 -1059 -163 -1058 -274 -666 -292 -715 -179 -928 -868 -894 -142 -677 -999 -1002 -758 -1010 -706 -120 -404 -177 -411 -772 -166 -808 -696 -771 -793 -1061 -1083 -134 -662 -1054 -240 -293 -367 -695 -408 -193 -617 -1022 -1043 -877 -424 -920 -331 -473 -957 -953 -674 -419 -179 -167 -523 -743 -1039 -959 -103 -1083 -212 -838 -159 -953 -295 -778 -451 -976 -1066 -165 -117 -510 -1052 -920 -670 -555 -500 -196 -737 -1018 -1000 -353 -161 -332 -1035 -898 -138 -659 -728 -149 -689 -916 -623 -313 -1026 -791 -538 -581 -913 -452 -888 -282 -888 -272 -735 -732 -456 -878 -943 -530 -730 -687 -876 -142 -559 -950 -182 -763 -757 -1069 -120 -523 -835 -1080 -524 -135 -880 -191 -650 -313 -979 -631 -918 -717 -786 -811 -148 -1021 -723 -290 -443 -1040 -945 -109 -985 -522 -159 -229 -703 -166 -894 -718 -447 -298 -334 -580 -953 -772 -620 -641 -336 -449 -790 -211 -1081 -545 -1058 -405 -925 -370 -440 -130 -1042 -622 -578 -691 -443 -244 -138 -924 -665 -646 -393 -276 -610 -1001 -623 -230 -245 -999 -755 -478 -588 -420 -268 -476 -277 -1008 -1065 -490 -908 -570 -634 -782 -367 -1058 -404 -428 -911 -732 -1029 -410 -1048 -1053 -893 -936 -545 -647 -760 -1010 -969 -834 -730 -1086 -573 -945 -656 -163 -959 -853 -664 -249 -230 -210 -971 -1059 -736 -742 -131 -687 -272 -367 -429 -933 -110 -619 -655 -398 -755 -907 -602 -960 -558 -223 -819 -916 -328 -360 -391 -198 -565 -414 -944 -610 -200 -417 -924 -291 -380 -598 -899 -738 -536 -372 -728 -504 -248 -658 -177 -1065 -665 -773 -1091 -230 -204 -125 -522 -868 -971 -621 -473 -537 -341 -947 -302 -346 -1012 -784 -671 -476 -294 -657 -880 -295 -843 -477 -644 -1097 -955 -407 -656 -694 -371 -174 -527 -518 -265 -590 -756 -461 -897 -486 -808 -495 -507 -229 -251 -775 -572 -683 -634 -410 -584 -1041 -834 -671 -583 -990 -566 -1032 -632 -430 -179 -327 -576 -1048 -926 -828 -126 -436 -924 -917 -889 -905 -782 -849 -642 -875 -238 -904 -512 -971 -327 -297 -342 -752 -841 -588 -653 -586 -170 -600 -866 -522 -658 -291 -1061 -1085 -302 -145 -200 -673 -247 -219 -188 -192 -868 -122 -954 -189 -1056 -483 -222 -654 -708 -900 -586 -527 -895 -187 -503 -908 -1055 -722 -173 -650 -248 -616 -578 -868 -538 -788 -582 -1061 -709 -778 -1030 -738 -1038 -420 -1069 -716 -852 -886 -282 -575 -596 -286 -477 -179 -852 -306 -1063 -198 -295 -523 -544 -991 -890 -837 -525 -980 -812 -713 -209 -820 -563 -553 -420 -182 -350 -220 -573 -685 -780 -137 -546 -948 -716 -247 -964 -576 -118 -1073 -779 -442 -503 -908 -1019 -651 -101 -604 -474 -457 -220 -582 -845 -301 -549 -399 -834 -634 -360 -727 -223 -110 -289 -759 -765 -560 -943 -297 -982 -763 -1071 -109 -996 -769 -655 -585 -131 -870 -924 -642 -960 -759 -732 -1028 -311 -630 -657 -851 -400 -490 -450 -133 -904 -650 -1004 -1069 -530 -113 -811 -550 -1029 -770 -558 -383 -435 -1020 -571 -341 -746 -465 -894 -103 -676 -639 -352 -823 -697 -965 -917 -220 -315 -1015 -309 -687 -986 -319 -196 -458 -144 -580 -1002 -315 -126 -980 -636 -256 -604 -744 -353 -1016 -752 -745 -680 -791 -422 -899 -631 -286 -1064 -403 -258 -559 -584 -805 -449 -137 -265 -739 -693 -494 -1064 -156 -417 -804 -160 -963 -371 -868 -1027 -564 -912 -379 -227 -937 -489 -893 -632 -251 -133 -996 -400 -330 -1010 -260 -1096 -725 -927 -846 -636 -1027 -1052 -700 -511 -641 -181 -984 -491 -537 -542 -541 -722 -953 -604 -169 -517 -183 -179 -995 -1020 -436 -734 -612 -859 -155 -709 -1022 -322 -672 -1045 -1026 -101 -619 -781 -498 -688 -607 -750 -571 -647 -305 -503 -521 -767 -729 -859 -975 -895 -681 -1089 -545 -893 -594 -909 -582 -919 -709 -721 -209 -274 -181 -329 -544 -688 -853 -740 -168 -329 -223 -922 -717 -878 -226 -263 -742 -952 -761 -945 -1043 -801 -552 -946 -207 -696 -530 -760 -726 -248 -497 -1069 -1050 -981 -431 -179 -497 -1068 -644 -873 -879 -680 -313 -543 -1063 -619 -561 -906 -263 -695 -953 -613 -647 -992 -490 -992 -567 -297 -339 -476 -519 -131 -413 -921 -339 -189 -292 -107 -852 -882 -1042 -774 -481 -578 -804 -582 -1008 -422 -819 -840 -1017 -291 -102 -797 -732 -783 -557 -163 -396 -645 -227 -613 -805 -302 -193 -1017 -884 -757 -106 -393 -495 -536 -287 -1080 -485 -833 -336 -360 -1057 -725 -777 -303 -573 -760 -701 -511 -867 -377 -255 -957 -224 -852 -969 -948 -231 -497 -814 -412 -257 -330 -838 -1063 -504 -616 -494 -477 -1057 -168 -184 -336 -965 -714 -830 -917 -561 -112 -394 -828 -427 -659 -537 -238 -530 -306 -917 -991 -395 -1065 -1039 -851 -726 -339 -788 -438 -868 -289 -515 -742 -768 -621 -698 -1028 -1096 -502 -838 -794 -683 -536 -598 -623 -804 -272 -973 -855 -441 -153 -467 -372 -283 -902 -502 -1064 -375 -735 -101 -911 -452 -848 -175 -802 -716 -535 -233 -927 -258 -262 -901 -649 -476 -770 -373 -409 -611 -396 -713 -887 -930 -695 -797 -279 -773 -1084 -538 -863 -192 -681 -228 -205 -211 -519 -494 -727 -1057 -800 -803 -581 -131 -1062 -728 -1018 -854 -762 -664 -593 -523 -102 -676 -662 -970 -710 -983 -600 -215
该输出用例:
-100
C++代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> const int maxn = 200002; int a[maxn],dp[maxn]; using namespace std; int main(){ int N; scanf("%d",&N); a[0] = 0; for(int i = 1; i <= N; i++){ scanf("%d",&a[i]); } int maxSum = -0x3f3f3f3f; memset(dp,0,sizeof(dp));
//修改的代码: for(int i = 1; i <= N; i++){ if(dp[i-1]>0) dp[i] = dp[i-1] + a[i]; else dp[i] = a[i]; } for(int i = 1; i <= N; i++){ if(maxSum < dp[i]) maxSum = dp[i]; } printf("%d ",maxSum); return 0; }