Codeforces Contest 1110 problem B Tape —— 由加变为减

You have a long stick, consisting of m segments enumerated from 1 to m. Each segment is 1 centimeter long. Sadly, some segments are broken and need to be repaired.

You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length t placed at some position s will cover segments s,s+1,…,s+t−1.

You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap.

Time is money, so you want to cut at most k continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces?

Input
The first line contains three integers n, m and k (1≤n≤105, n≤m≤109, 1≤k≤n) — the number of broken segments, the length of the stick and the maximum number of pieces you can use.

The second line contains n integers b1,b2,…,bn (1≤bi≤m) — the positions of the broken segments. These integers are given in increasing order, that is, b1<b2<…<bn.

Output
Print the minimum total length of the pieces.

Examples
inputCopy
4 100 2
20 30 75 80
outputCopy
17
inputCopy
5 100 3
1 2 4 60 87
outputCopy
6
Note
In the first example, you can use a piece of length 11 to cover the broken segments 20 and 30, and another piece of length 6 to cover 75 and 80, for a total length of 17.

In the second example, you can use a piece of length 4 to cover broken segments 1, 2 and 4, and two pieces of length 1 to cover broken segments 60 and 87.

题意:

给你一些洞,你要用最多k根木条将所有的洞补上,问你木条长度最少的方法是多少。

题解:

一开始是想着排序然后贪心的一点一点加的,但是总有问题,交上去不出意料也wa了,后来想到应该是一开始就用一根木条覆盖所有的,然后第二根木条来的时候就可以把空隙最大的那一块去掉,以此类推就ok了

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
int p[N],np[N],cnt;
void init()
{
    for(ll i=2;i<N;i++)
    {
        if(!np[i])
        {
            p[++cnt]=i;
            for(ll j=i*i;j<N;j+=i)
                np[j]=1;
        }
    }
}
int main()
{
    init();
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int a;
        scanf("%d",&a);
        int ret=1,cnt=0;
        while(ret<=a)
            ret*=2,cnt++;
        if(ret-1==a)
        {
            if(cnt%2==0)
            {
                int ans=0;
                for(int r=1;r<ret;r<<=2)
                    ans+=r;
                printf("%d\n",ans);
            }
            else
            {
                int ans=a;
                for(int i=1;i<=cnt&&p[i]<=sqrt(a);i++)
                {
                    if(a%p[i]==0)
                    {
                        ans=p[i];
                        break;
                    }
                }
                printf("%d\n",a/ans);
            }
        }
        else
            printf("%d\n",ret-1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值