SoJ 4390 Elevator Problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.