Background: The weekly question was not read at the time. Even if you read it, you can only think of violence, not AC.
Learning: 1. In the case of violent search timeouts, a good algorithm must be found, which is to look at the optimal solution with a similar trend of change rather than figure out the corresponding value for each floor and find the maximum value. Train of thought: Assume current floor below has N1 person, current floor has N2 person, current floor above has N3 person. Every step up, there's n1+n2 people going to take the first floor.
People want to walk the first floor, if starting from the No. 0 floor to consider, then N1+N2 is 0,n3 for the total number of people, and then go upstairs, N1+n2 monocytogenes, N3 single minus, obviously N1+n2>=n3 stop upstairs is to take maximum value.
Code:
#include <stdio.h> #include <stdlib.h> #include <math.h>short str[1000000];int main () { int t; scanf ("%d", &t); while (t--) { int n,floor,n1=0,n2=0,n3,sum=0; Long long people=0; scanf ("%d", &n); for (int i=0;i<n;i++) { scanf ("%hd", &str[i]); Sum+=str[i]; } for (int i=0;i<n;i++) { if (i>0) n1+=str[i-1]; N2=str[i]; N3=SUM-N1-N2; if (n1+n2-n3>=0) { floor=i+1; for (int k=0;k<n;k++) people+=str[k]* (ABS (I-K)); break; } } printf ("%d%lld\n", floor,people); } return 0;}
SoJ 4390 Elevator Problem