Codeforces 462C Appleman and Toastman Problem solving report

Source: Internet
Author: User

Title Link: Http://codeforces.com/problemset/problem/461/A

The title means: give a group of n number of sets of you, in turn, loop to perform two operations:

(1) Each time Toastman gets a set, he calculates the count and adds it to the score. He then passed the collection to Appleman.

(2) Appleman get the set if there is only one number, discard it, otherwise the set is divided into two disjoint and not empty collection, passed back to Toastman.

These operations continue until the number of collections becomes 0, which means that the collection is discarded by turning it into only one element. Ask how the partitioning makes score the largest.

In fact, by simulating these two operations can find out how to divide to make the largest score. That is, each time it is divided into two sets, it is guaranteed that one of the elements containing only a single element of the set is the one that is not pre-divided and that is the smallest. For example {1, 3, 8, 7} The first division into {1}, {3, 8, 7}, the second is {3}, {8, 7} ({1} This set has been discarded according to test instructions), and the third time is {7}, {8}. have found that 1 this number was added two times, 3 was added three times, 7 was added four times, 8 was added four times? Right!! This is the law of problem solving.

My practice is, the edge of the input edge statistics of the entire set of the and, that is, at the beginning of the group to add to the score, and then from small to large order, for the number of I is a[i] multiply I (the cycle is initially starting from 1), added to the score, note, for the last division, that is, the only two {an-1, an}, for A[n], it is not multiplied by N times, but n-1 times. Another area to note is that score is going to use a long long. Because it's possible > 3e5*1e6, it's pretty big.

Last night, due to a (reading the wrong question), B (was hack) did poorly, do C only left 20min, there is a little thought, too late to finish. The lesson is to pay attention to the data range, long long processing!!!

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <algorithm>6 using namespacestd;7 8typedefLong LongLL;9 Const intMAXN = 3e5 +5;Ten LL A[MAXN]; One  A intMain () - { -     intN; the      while(SCANF ("%d", &n)! =EOF) -     { -LL s =0; -          for(inti =1; I <= N; i++) +         { -scanf"%lld", &a[i]); +s + =A[i]; A         } atSort (A +1, A +1+n); -          for(inti =1; I < n; i++) -S + = (LL) a[i] * (LL) I;//because before the sum, so not a[i]* (i+1) -s + = (LL) (n1)*(LL) a[n]; -printf"%lld\n", s); -     } in     return 0; -}
View Code

Codeforces 462C Appleman and Toastman Problem solving report

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.