Hello 2022 B. Integers Shop

博客作者分享了在Codeforces(CF)比赛中遇到连续失败的经历,并详细分析了一道涉及线段购买整数的动态规划题目。通过分类讨论和贪心策略,找到最优解。作者探讨了如何在限制条件下找到覆盖最广泛整数的最小花费,并提供了可能的优化思路。
摘要由CSDN通过智能技术生成

麻了,又开始fastfoces了。 更可气的是B差一点就ac了,如果再多给我几分钟那我就直接起飞了,可是并没有,所以我坠落了。。。
题意:
有条线段,用c价格的钱可以买下l到r之间的整数,给出n组数据,输出能得到最多整数的情况需要的花费,如果有相同整数的情况则输出花费较小的值,同一个整数只能获取一次。
分析:
剪两次,中间的会被当做礼物赠送。答案最多是剪两次的情况,一次剪r最大的时候,一次剪l最小的时候,剩下的中间部分会直接赠送给我们,然后我们找到价格最小同时又能覆盖到最小的l到最大的r部分的情况就是答案。
代码:
我用的是贪心+分类讨论,代码极繁琐,肯定有大佬的要比我的简单很多。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define endl '\n';
int main()
{
    ll n,t,i,j;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ll l,r,c;
        ll sum=0;
        ll lmin,rmax;
        ll fuzhu1,fuzhu2;//l最小并且花费最少的值,r最大并且花费最少的值
        cin>>l>>r>>c;
        fuzhu1=c,fuzhu2=c;
        sum=c;
        lmin=l;
        rmax=r;
        cout<<sum<<endl;  //第一组数据答案就是第一个
        for(i=1; i<n; i++)
        {
            cin>>l>>r>>c;
            if(l<lmin&&r>rmax) //如果l是最小的r是最大的那么它本身
                               //就可以取到最多的整数,这就是答案
            {
                lmin=l;
                rmax=r;
                fuzhu1=c,fuzhu2=c;
                sum=c;
                cout<<sum<<endl;
                continue;
            }
            if(l<lmin&&r==rmax) 
            //这也同样是答案,但是我们要为后面的数据做准备,递推算出最大的r中需要花费最少的情况
            {
                lmin=l;
                rmax=r;
                fuzhu1=c;  //l是最小的,左边就是最好的情况
                fuzhu2=min(c,fuzhu2);
                sum=c;
                cout<<sum<<endl;
                continue;
            }
            if(l==lmin&&r>rmax)
            {
                lmin=l;
                rmax=r;
                fuzhu1=min(c,fuzhu1);//递推算出最小的l中需要花费最少的情况
                fuzhu2=c; //r是最大的,右边就是最好的情况
                sum=c;
                cout<<sum<<endl;
                continue;
            }
            if(l<lmin&&r<rmax)
            //虽然l最小但是r不是最大的,我们要找到r最大的并且花费最少的情况,前面已经递推出来了
            {
                lmin=l;
                fuzhu1=c;
                sum=c+fuzhu2;
                cout<<sum<<endl;
                continue;
            }
            if(l>lmin&&r>rmax) //和上一个if同理
            {
                rmax=r;
                fuzhu2=c;
                sum=c+fuzhu1;
                cout<<sum<<endl;
                continue;
            }
            if(l>lmin&&r<rmax) 
            //l不是最小,r不是最大,这不符合我们的贪心策略,中间的部分我们只要让它成为礼物就行了
            {
                cout<<sum<<endl;
                continue;
            }
            if(l==lmin&&r<rmax) 
            //这种情况可能会改变最小l花费的最小值,需要递推一下
            {
                fuzhu1=min(fuzhu1,c);
                sum=min(sum,c+fuzhu2);
                cout<<sum<<endl;
                continue;
            }
            if(l>lmin&&r==rmax)//和上一个if同理,更改的是最大的r的情况
            {
                fuzhu2=min(fuzhu2,c);
                sum=min(sum,c+fuzhu1);
                cout<<sum<<endl;
                continue;
            }
            if(l==lmin&&r==rmax)
            {
                fuzhu1=min(fuzhu1,c);
                fuzhu2=min(fuzhu2,c);
                sum=min(sum,c);
                cout<<sum<<endl;
                continue;
            }
        }
    }
}

连续六次cf掉分,有没有大佬给点建议救一下菜鸟

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Integer slices are a way to extract a subset of elements from a list or array of integers. In Python, you can use the slice notation to extract a range of elements from a list or array. For example: ``` my_list = [1, 2, 3, 4, 5] slice_1 = my_list[1:3] # returns [2, 3] slice_2 = my_list[2:] # returns [3, 4, 5] slice_3 = my_list[:4] # returns [1, 2, 3, 4] ``` In the above examples, `my_list` is a list of integers. The slice notation `[1:3]` extracts the elements at index 1 and 2 (not including 3), which are `2` and `3`. The slice notation `[2:]` extracts all elements from index 2 to the end of the list. The slice notation `[:4]` extracts all elements from the beginning of the list up to (but not including) index 4. Integer slices can also be used with arrays of integers in languages such as C and Java. In these languages, you can use the array indexing notation to extract a range of elements from an array. For example: ``` int my_array[] = {1, 2, 3, 4, 5}; int slice_1[] = Arrays.copyOfRange(my_array, 1, 3); // returns [2, 3] int slice_2[] = Arrays.copyOfRange(my_array, 2, my_array.length); // returns [3, 4, 5] int slice_3[] = Arrays.copyOfRange(my_array, 0, 4); // returns [1, 2, 3, 4] ``` In the above examples, `my_array` is an array of integers. The `Arrays.copyOfRange` method is used to extract a range of elements from the array. The first argument is the array to extract from, the second argument is the starting index, and the third argument is the ending index (not including).
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值