As a fan of Doudizhu, WYJ likes collecting playing cards very much.
One day, MJF takes a stack of cards and talks to him: let's play a game and if you win, you can get all these cards. MJF randomly assigns these cards into
One day, MJF takes a stack of cards and talks to him: let's play a game and if you win, you can get all these cards. MJF randomly assigns these cards into
InputThere are about bi.
OutputFor each test case, print only an integer, denoting the number of piles WYJ needs to move before the game starts. If there are multiple solutions, print the smallest one.
Sample Input
5 4 6 2 8 4 1 5 7 9 2
Sample Output
4
Hint
[pre]
For the sample input:
+ If WYJ doesn't move the cards pile, when the game starts the state of cards is:
4 6 2 8 4
1 5 7 9 2
WYJ can take the first three piles of cards, and during the process, the number of face-up cards is 4-1+6-5+2-7. Then he can't pay the the "penalty value" of the third pile, the game ends. WYJ will get 12 cards.
+ If WYJ move the first four piles of cards to the end, when the game starts the state of cards is:
4 4 6 2 8
2 1 5 7 9
WYJ can take all the five piles of cards, and during the process, the number of face-up cards is 4-2+4-1+6-5+2-7+8-9. Then he takes all cards, the game ends. WYJ will get 24 cards.
It can be improved that the answer is 4.
**huge input, please use fastIO.**
[/pre]
给你两个数组,数组的元素和相等
让你将a[i]-b[i]的值的和最大
其实只要找到最后一段(a[i]-b[i])和小于0的点就行了
下面上代码
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 int a[1000010],b[1000010]; 7 int main() { 8 int n; 9 while(scanf("%d",&n)!=EOF){ 10 for (int i=0 ;i<n ;i++ ) 11 scanf("%d",&a[i]); 12 for (int i=0 ;i<n ;i++) 13 scanf("%d",&b[i]); 14 int sum=0,ans=0; 15 for (int i=0 ;i<n ;i++ ){ 16 sum+=(a[i]-b[i]); 17 if (sum<0) { 18 ans=i+1; 19 sum=0; 20 } 21 } 22 printf("%d\n",ans); 23 } 24 return 0; 25 }