Codeforces 761D-Dasha and Very Difficult Problem 贪心

参考http://blog.csdn.net/a664607530/article/details/54822162

贪心:

题意:数组a和数组b中每个元素都是不小于l,不大于r,数组c由数组b中元素减去数组a中对应位置的元素得到,即ci = bi - ai,数组p表示相应位置的数组c中的元素在数组c中排第几,现在告诉你l,r和数组a,数组p,若能得到数组b

,则任写一组数组b,否则输出-1

解题思路:用结构体的形式来保存数组a,数组b,数组p,数组c中的各个元素,先以数组p来排序,则第一个b元素应

尽量小,所以可以定为l,后面的元素可以以此来推,若推出来的b元素小于l,则可以将其变大至l,若大于r,则说明

无法找到这样的数组b

心得:这个构造还是贪心对。不过要注意

    #include <iostream>  
    #include <cstdio>  
    #include <string>  
    #include <cstring>  
    #include <algorithm>  
    #include <cmath>  
    #include <queue>  
    #include <vector>  
    #include <set>  
    #include <stack>  
    #include <map>  
    #include <climits>  

    using namespace std;  

    #define LL long long  
    const int INF=0x3f3f3f3f;  

    struct node  
    {  
        int a,b,p,c;  
        int id;  
    }x[100009];  
    int n,l,r;  

    bool cmp(node xx,node yy)  
    {  
        return xx.p<yy.p;  
    }  

    bool cmp1(node xx,node yy)  
    {  
        return xx.id<yy.id;  
    }  

    int main()  
    {  
        while(~scanf("%d %d %d",&n,&l,&r))  
        {  
            for(int i=1;i<=n;i++)  
            {  
                scanf("%d",&x[i].a);  
                x[i].id=i;  
            }  
            for(int i=1;i<=n;i++)  
                scanf("%d",&x[i].p);  
            sort(x+1,x+1+n,cmp);  
            int flag=1;  
            for(int i=1;i<=n;i++)  
            {  
                if(i==1) x[i].b=l,x[i].c=l-x[i].a;  
                else  
                {  
                    x[i].c=x[i-1].c+1;  
                    x[i].b=x[i].a+x[i].c;  
                    if(x[i].b<l) x[i].b=l,x[i].c=l-x[i].a;  
                    if(x[i].b>r) flag=0;  
                }  
                if(flag==0) break;  
            }  
            if(!flag) printf("-1\n");  
            else  
            {  
                sort(x+1,x+1+n,cmp1);  
                printf("%d",x[1].b);  
                for(int i=2;i<=n;i++)  
                    printf(" %d",x[i].b);  
                printf("\n");  
            }  
        }  
        return 0;  
    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值