首页 > 其他分享 >C. Ticket Hoarding

C. Ticket Hoarding

时间:2024-04-09 16:58:21浏览次数:21  
标签:tickets limits color Hoarding ticket sum Ticket day

C. Ticket Hoarding

As the CEO of a startup company, you want to reward each of your $k$ employees with a ticket to the upcoming concert. The tickets will be on sale for $n$ days, and by some time travelling, you have predicted that the price per ticket at day $i$ will be $a_i$. However, to prevent ticket hoarding, the concert organizers have implemented the following measures:

  • A person may purchase no more than $m$ tickets per day.
  • If a person purchases $x$ tickets on day $i$, all subsequent days (i.e. from day $i+1$ onwards) will have their prices per ticket increased by $x$.

For example, if $a = [1, 3, 8, 4, 5]$ and you purchase $2$ tickets on day $1$, they will cost $2$ in total, and the prices from day $2$ onwards will become $[5, 10, 6, 7]$. If you then purchase $3$ more tickets on day $2$, they will cost in total an additional $15$, and the prices from day $3$ onwards will become $[13, 9, 10]$.

Find the minimum spending to purchase $k$ tickets.

Input

Each test contains multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains three integers $n$, $m$, and $k$ ($1 \le n \le 3 \cdot 10^5, 1 \le m \le 10^9, 1 \le k \le \min(nm, 10^9)$) — the number of sale days, the maximum amount of ticket purchasable each day, and the number of tickets to be bought at the end.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the price per ticket for each of the upcoming $n$ days.

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

Output

For each test case, print one integer: the minimum amount of money needed to purchase exactly $k$ tickets.

Example

input

4
4 2 3
8 6 4 2
4 2 8
8 6 4 2
5 100 1
10000 1 100 10 1000
6 3 9
5 5 5 5 5 5

output

10
64
1
72

Note

In the first test case, one optimal way to buy $3$ tickets is as follows:

  • Buy $0$ tickets on the first day. The prices per ticket for the remaining days are $[6, 4, 2]$.
  • Buy $0$ tickets on the second day. The prices per ticket for the remaining days are $[4, 2]$.
  • Buy $1$ ticket on the third day with cost $4$. The price per ticket for the remaining day is $[3]$.
  • Buy $2$ tickets on the fourth day with cost $6$.

In the second test case, there is only one way to buy $8$ tickets:

  • Buy $2$ tickets on the first day with cost $16$. The prices per ticket for the remaining days are $[8, 6, 4]$.
  • Buy $2$ tickets on the second day with cost $16$. The prices per ticket for the remaining days are $[8, 6]$.
  • Buy $2$ tickets on the third day with cost $16$. The price per ticket for the remaining day is $[8]$.
  • Buy $2$ tickets on the fourth day with cost $16$.

 

解题思路

  直观的贪心思路是,选择最小的 $x = \left\lfloor \frac{k}{m} \right\rfloor$ 个 $a_i$ 购买,其中前 $x-1$ 个 $a_i$ 购买的数量均为 $m$,第 $x$ 个 $a_i$ 购买的数量为 $k - (x-1)\cdot m$。下面给出证明。

  用 $b_i \, (0 \leq b_i \leq m)$ 表示购买 $a_i$ 的数量,且 $\sum\limits_{i=1}^{n}{b_i} = k$。购买 $a_i$ 的代价就是 $\left( a_i + \sum\limits_{j=1}^{i-1}{b_j} \right) b_i$。因此总代价就是:

\begin{align*}
&\sum_{i=1}^{n}{\left( a_i + \sum_{j=1}^{i-1}{b_j} \right) b_i} \\
=& \sum_{i=1}^{n}{a_ib_i} + \sum_{i=1}^{n}{b_i \sum_{j=1}^{i-1}{b_j}} \\
\end{align*}

  可以证明,将 $(a_i, a_j)$ 与 $(a_j, b_j)$ 进行交换,上述等式的值不变。其中 $\sum\limits_{i=1}^{n}{a_ib_i}$ 这部分显然没有变化,下面证明 $\sum\limits_{i=1}^{n}{b_i \sum\limits_{j=1}^{i-1}{b_j}}$ 这部分也是不变的。

  不失一般性,假设 $i \leq j$,将 $\sum\limits_{i=1}^{n}{b_i \sum\limits_{j=1}^{i-1}{b_j}}$ 根据 $i$ 和 $j$ 分成三部分的和 $\sum\limits_{k=1}^{i-1}{b_k \sum\limits_{u=1}^{k-1}{b_u}} + \sum\limits_{k=i}^{j}{b_k \sum\limits_{u=1}^{k-1}{b_u}} + \sum\limits_{k=j+1}^{n}{b_k \sum\limits_{u=1}^{k-1}{b_u}}$。其中 $\sum\limits_{k=1}^{i-1}{b_k \sum\limits_{u=1}^{k-1}{b_u}}$ 这部分显然没有变化,因为没有涉及到 $b_i$ 和 $b_j$。$\sum\limits_{k=j+1}^{n}{b_k \sum\limits_{u=1}^{k-1}{b_u}}$ 这部分也没有变化,因为 $b_i$ 和 $b_j$ 只会出现在 $\sum\limits_{u=1}^{k-1}{b_u}$ 中,而顺序并不会影响求和的结果。

  剩下的就是 $\sum\limits_{k=i}^{j}{b_k \sum\limits_{u=1}^{k-1}{b_u}}$,在 $(a_i, a_j)$ 与 $(a_j, b_j)$ 交换后,考虑 ${\color{Red} {b_i \sum\limits_{u=1}^{i-1}{b_u}}}$,${\color{Green} {\sum\limits_{k=i+1}^{j-1}{b_k \sum\limits_{u=1}^{k-1}{b_u}}}}$,${\color{Blue} {b_j \sum\limits_{u=1}^{i-1}{b_u}}}$ 这三部分发生的变化:

\begin{align*}
&{\color{Red} {b_j\sum_{u=1}^{i-1}{b_u}}} - {\color{Red} {b_i\sum_{u=1}^{i-1}{b_u}}} + {\color{Blue} {b_i\sum_{u=1}^{j-1}{b_u}}} - {\color{Blue} {b_i^2}} + {\color{Blue} {b_ib_j}} - {\color{Blue} {b_j\sum_{u=1}^{j-1}{b_u}}} + {\color{Green} {b_j\sum\limits_{k=i+1}^{j-1}{b_k}}} - {\color{Green} {b_i\sum\limits_{k=i+1}^{j-1}{b_k}}}  \\
=& (b_i - b_j)\sum_{u=1}^{j-1}{b_u} - (b_i - b_j)\sum_{u=1}^{i-1}{b_u} - (b_i - b_j)\sum\limits_{u=i+1}^{j-1}{b_u} - b_i(b_i - b_j) \\
=& (b_i - b_j)\sum_{u=i}^{j-1}{b_u} - (b_i - b_j)\sum\limits_{u=i+1}^{j-1}{b_u} - b_i(b_i - b_j) \\
=& (b_i - b_j)b_i - b_i(b_i - b_j) \\
=& 0
\end{align*}

  这意味着我们当我们确定购买的方案 $b_i$ 后,以任意顺序购买 $a_i$,得到的代价都不变。对 $a_i$ 进行升序排序,并构造方案 $b = [ \underbrace{m,m,\ldots,m}_{x-1}, k - (x-1)\cdot m, 0, 0, \ldots, 0]$。可以证明该购买方案得到的代价最小。

  其中 $\sum\limits_{i=1}^{n}{a_ib_i}$ 这部分根据排序不等式显然在 $0 \leq b_i \leq m$ 且 $\sum\limits_{i=1}^{n}{b_i}=k$ 的限制下,得到的结果最小。而另外一部分由于 $\left(\sum\limits_{i=1}^{n}{b_i}\right)^2 = \sum\limits_{i=1}^{n}{b_i^2} +  2\sum\limits_{i=1}^{n}{b_i \sum\limits_{j=1}^{i-1}{b_j}}$,从而推出 $\sum\limits_{i=1}^{n}{b_i \sum\limits_{j=1}^{i-1}{b_j}} = \frac{1}{2} k^2 - \frac{1}{2} \sum\limits_{i = 1}^{n}{b_i^2}$。从而只需证明 $\sum\limits_{i = 1}^{n}{b_i^2}$ 得到的结果最大。如果存在两个 $i$,$j$ 满足 $0 < b_i \leq b_j < m$,那么用 $(b_i-1, b_j+1)$ 取代 $(b_i,b_j)$,则 $(b_i-1)^2 + (b_j+1)^2 = b_i^2 + b_j^2 + 2(b_j - b_i + 1) > b_i^2 + b_j^2$ 即结果会变大。容易知道当 $b_i$ 为上述方案时,$\sum\limits_{i = 1}^{n}{b_i^2}$ 的结果最大,从而 $\frac{1}{2} k^2 - \frac{1}{2} \sum\limits_{i = 1}^{n}{b_i^2}$ 的结果最小。

  证毕。

  AC 代码如下,时间复杂度为 $O(n \log{n})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 3e5 + 5;

int a[N];

void solve() {
    int n, m, k;
    scanf("%d %d %d", &n, &m, &k);
    for (int i = 0; i < n; i++) {
        scanf("%d", a + i);
    }
    sort(a, a + n);
    LL ret = 0;
    for (int i = 0, s = 0; i < n; i++) {
        int t = min(k, m);
        ret += (LL)t * (a[i] + s);
        k -= t;
        s += t;
    }
    printf("%lld\n", ret);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Global Round 25 Editorial:https://codeforces.com/blog/entry/128116

标签:tickets,limits,color,Hoarding,ticket,sum,Ticket,day
From: https://www.cnblogs.com/onlyblues/p/18122678

相关文章

  • 【前端素材】推荐优质电影票购票商城网站设计Ticket平台模板(附源码)
     一、需求分析1、功能分析在线电影票购票商城是指一个通过互联网提供电影票购买服务的平台。它通常包括以下功能:电影信息展示:商城会展示当前热映电影、即将上映电影和影片详情,包括电影名称、演员阵容、导演、剧情简介、上映时间等信息,帮助用户选择电影。影院选择和座位......
  • A. Rudolf and the Ticket
    题解简单的二分应用,对于每个bi我们只需找到最大的ci使得bi+ci<=target即可code #include<bits/stdc++.h>usingnamespacestd;inta[105],b[105];intmain(){//freopen("input.txt","r",stdin);intt;cin>>t;while(t--){int......
  • Buy Tickets
    BuyTicketsRailwayticketsweredifficulttobuyaroundtheLunarNewYearinChina,sowemustgetupearlyandjoinalongqueue…TheLunarNewYearwasapproaching,butunluckilytheLittleCatstillhadschedulesgoinghereandthere.Now,hehadt......
  • MongoDB 7.0 动态 WiredTiger tickets
    在WiredTiger存储引擎中,WiredTigertickets提供了并发控制机制。这些tickets分为读tickets和写tickets。当多个操作,比如读和写尝试并发访问数据库,WiredTiger使用tickets来确保这些操作不会冲突,从而保证数据的完整性和性能。WiredTiger中的"tickets"实际上是一种资源管理机制,用于限......
  • MongoDB WiredTiger的读/写ticket
    在WiredTiger中,读/写ticket控制着并发性。也就是说,读/写ticket控制着有多少读写操作可以同时在存储引擎上执行。这是WiredTiger特有的设置,因此不会影响数据库中并发操作的数量。MongoDB有单独的机制来保存操作进度,可以退让给其他操作。 默认值读/写ticket的默认值都是128。这......
  • 「BZOJ2505」tickets 题解
    preface网上目前还没看到我的方法,就大概讲一下做法solution首先想到贪心,考虑\([l,r]\)的最大次数,一定是找到最小的\(x\)满足\(l\simx\)的位数的和大于等于\(k\),然后递归的求解\([x+1,r]\),易证。还是考虑将\(Query(l,r)\)拆分成\(Query(1,r)\)和\(Query......
  • 什么是 Service Ticket 的 Service Level Agreement
    服务工单(Ticket)的服务级别协议(ServiceLevelAgreement,SLA)是一个重要的概念,特别是在提供技术支持和客户服务的领域中。服务工单是组织内或与客户之间的通信记录,用于跟踪问题、请求或任务的处理。SLA是一种协议或承诺,其中规定了一组指标和参数,以确保服务工单得到适时、高质量的处理......
  • cas入门之二十四:ticket的过期策略
    cas提供了可插拔式的ticket过期策略框架用于tgt和st。在cas应用中,tgt和st的过期策略配置默认在cas/webapp/WEB-INF/spring-configuration/ticketExpirationPolicies.xml文件中。在cas的过期策略中,并没有明确指出哪一种ticket应用于哪一种过期策略,但是我们根据类名,还是能够进行区分......
  • 针对jsapi_ticket不能频繁刷新,缓存的几种方式
    正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。由于获取jsapi_ticket的api调用次数非常有限,频繁刷新jsapi_ticket会导致api调用受限,影响自身业务,开发者必须在自己的服务全局缓存jsapi_ticket。 在.NETCore中,你可以使用内置的缓存系统来管理和操作缓存数......
  • POJ - Buy Tickets
    Smiling&Weeping----你看这个人,嘴里说着喜欢我却又让我这么难过DescriptionRailwayticketsweredifficulttobuyaroundtheLunarNewYearinChina,sowemustgetupearly......