Codeforces 657c-bear and contribution [idea title]

Source: Internet
Author: User

Title Link: Http://


The special thing about the topic is that only $+1$ $+5$ these two operations we have to consider how to use this condition

When you think about it, you can see that if the target value of the optimal solution is $x ($ to increase the value of at least $k$ personal to $x) $

Then there must be a man whose initial value is within the range of $[x-4, x]$

Otherwise, the $x$ minus the $5$ can get a better solution.

Therefore, the target value that may be the optimal solution is only $n * 5$ species

Now consider how to quickly calculate an answer for each target value in the context of $O (N) $ enumeration of target values

My first idea was to write the $5$ in terms of the remainder of the $mod 5$ to record the prefixes and whatever

However such two-point answer has a $log $ two fractional set subscript another $log $

Although the topic gave $4s$ should be able to pass but always feel that this is not elegant

Re-analysis of the condition of the problem we can find that each query is actually the minimum distance from the target $k $ number and this $k$ is unchanged

So now the problem is to maintain a data structure that supports finding the smallest number of $k in a container and adding a number

This is obviously a heap of total complexity is $O (n + nlogk) $

The sort is not the same as the cost of transferring all the initial values to the target value in the case of the 5$ $5$ $mod

So we're just going to keep $5$ a heap.

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5#include <queue>6 using namespacestd;7 Const intN = 2e5 +Ten;8 Const Long LongINF =1e9;9 intA[n], Dis[n *5];Ten Long Longsum[5]; One Long LongAns =1e18; A intN, K, B, C, CNT; -Priority_queue <Long Long> q[5]; - intMain () the { -scanf"%d%d%d%d", &n, &k, &b, &c); -b = min (b, c *5); -      for(inti =1; I <= N; ++i) +     { -scanf"%d", &a[i]); +          for(intj =0; J <5; ++j) Adis[cnt++] = A[i] +J; at     } -Sort (A +1, A +1+n); -Sort (dis, dis +CNT); -CNT = unique (dis, dis + cnt)-dis; -     intnow =1; -      for(inti =0; I < CNT; ++i) in     { -         intx = Dis[i], kind = (inf + x)%5; to         Long LongCost ; +          while(now <= n && A[now] <=x) -         { the              for(intj =0; J <5; ++j) *             { $Cost = (INF + j-a[now])/5* B + (INF + j-a[now])%5*C;Panax Notoginseng Q[j].push (cost); -SUM[J] + =Cost ; the                 if((int) Q[j].size () >k) +                 { ASUM[J]-=q[j].top (); the Q[j].pop (); +                 } -             } $++Now ; $         } -         Long Longy = (inf + kind-x)/5* b *K; -         if((int) q[kind].size () = =k) theans = min (ans, sum[kind)-y); -     }Wuyiprintf"%lld\n", ans); the     return 0; -}

Codeforces 657c-bear and contribution [idea title]

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: 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.