CodeForces779C--Dishonest Sellers

C. Dishonest Sellers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi.

Not all of sellers are honest, so now some products could be more expensive than after a week of discounts.

Igor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items.
Input

In the first line there are two positive integer numbers n and k
(1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — total number of items to buy and minimal
number of items Igor wants to by right now.

The second line contains sequence of integers a1, a2, …, an
(1 ≤ ai ≤ 104) — prices of items during discounts (i.e. right now).

The third line contains sequence of integers b1, b2, …, bn
(1 ≤ bi ≤ 104) — prices of items after discounts (i.e. after a week).

Output

Print the minimal amount of money Igor will spend to buy all n items.
Remember, he should buy at least k items right now. Examples

Input

3 1

5 4 6

3 1 5

Output

10

Input

5 3

3 4 7 10 3

4 5 5 12 5

Output

25

Note

In the first example Igor should buy item 3 paying 6. But items 1 and
2 he should buy after a week. He will pay 3 and 1 for them. So in
total he will pay 6 + 3 + 1 = 10.

In the second example Igor should buy right now items 1, 2, 4 and 5,
paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy
after a week of discounts, he will pay 5 for it. In total he will
spend 3 + 4 + 10 + 3 + 5 = 25.

translation:
全都得买,先至少买m个a状态的,若还有涨价的继续买,
否则买降价的状态的。

话说总是自己写的快排慢。。。
AC:

  #include<stdio.h>  
    #include<string.h>  
    #include<algorithm>  
    using namespace std;  
    int c[200050];  
    int a[200050];  
    int b[200050];  
    int main()  
    {  
        int n,m;  
        while(~scanf("%d%d",&n,&m))  
        {  
            int sum=0;  
            for(int i=1;i<=n;i++)scanf("%d",&a[i]);  
            for(int i=1;i<=n;i++)scanf("%d",&b[i]),sum+=b[i];  默认买b
            for(int i=1;i<=n;i++)c[i]=b[i]-a[i];  
            sort(c+1,c+n+1);  
            reverse(c+1,c+n+1);  
            for(int i=1;i<=n;i++)  
            {  
                if(i<=m)sum-=c[i];//至少买m个a,买涨价涨的多的
                else  
                {  
                    if(c[i]>=0)sum-=c[i];  接着买涨价了的
                    else break;  
                }  
            }  
            printf("%d\n",sum);  
        }  
    }  

AC:
这个贪心更明显

    #include<bits/stdc++.h>
    using namespace std;  
    struct itemm  
    {  
        int oldd;  
        int neww;  
    }item[200005];  
    bool cmp(itemm a,itemm b)  
    {  
        return a.neww-a.oldd>b.neww-b.oldd;  
    }  
    int main()  
    {  
        int n,k;  
        int key;  
        while(scanf("%d %d",&n,&k)==2)  
        {  
            key=0;  
            for(int i=0;i<n;i++)  
                scanf("%d",&item[i].oldd);  
            for(int i=0;i<n;i++)  
                scanf("%d",&item[i].neww);  
            sort(item,item+n,cmp);  
            for(int i=0;i<n;i++)  
            {  
                if(item[i].neww>=item[i].oldd)  
                    key+=item[i].oldd;  
                else if(item[i].neww<item[i].oldd&&k<=0)  
                    key+=item[i].neww;  
                else if(item[i].neww<item[i].oldd&&k>0)  
                    key+=item[i].oldd;  
                k--;  
            }  
            cout<<key<<endl;  
        }  
        return 0;  
    }  

TLE

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int a,b,c;int t;int f;
}y[223456];
void qsort(struct node y[],int l,int r)
{
    int i=l,j=r;
    struct node x=y[i];
    if(l>=r)  return ;
    while(i<j)
    {
        while(i<j&&y[j].c>=x.c)  j--;
        y[i]=y[j];
        while(i<j&&y[i].c<=x.c)  i++;
        y[j]=y[i];
    }
    y[i]=x;
    qsort(y,l,i-1);
    qsort(y,i+1,r);
}
int main()
{
    int T;
    int i,j;
    int n,k;
    while(cin>>n>>k)
    {
        for(i=0;i<n;i++)
            {scanf("%d",&y[i].a);y[i].f=0;}
        for(i=0;i<n;i++)
        {
            y[i].t=1;
            scanf("%d",&y[i].b);
            y[i].c=y[i].a-y[i].b;
            if(y[i].c<0) y[i].f=1;
        }
        qsort(y,0,n-1);
        int sum=0;
        for(i=0;i<k;i++)
        {
            sum+=y[i].a;
            y[i].t=0;
        }
        for(i=k-1;i<n;i++)
        {
            if(y[i].t==1&&y[i].f==1)
            {
                sum+=y[i].a;
                y[i].t=0;
            }
            if(y[i].t)
            {
            sum+=y[i].b;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

TLE改进后AC:

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int a,b,c;int t;int f;
}y[223456];
bool cmp(struct node x,struct node y)
{
    return x.c<y.c;
}
int main()
{
    int T;
    int i,j;
    int n,k;
    while(cin>>n>>k)
    {
        for(i=0;i<n;i++)
            {scanf("%d",&y[i].a);y[i].f=0;}
        for(i=0;i<n;i++)
        {
            y[i].t=1;
            scanf("%d",&y[i].b);
            y[i].c=y[i].a-y[i].b;
            if(y[i].c<0) y[i].f=1;
        }
        sort(y,y+n,cmp);
        int sum=0;
        for(i=0;i<k;i++)
        {
            sum+=y[i].a;
            y[i].t=0;
        }
        for(i=k-1;i<n;i++)
        {
            if(y[i].t==1&&y[i].f==1)
            {
                sum+=y[i].a;
                y[i].t=0;
            }
            if(y[i].t)
            {
            sum+=y[i].b;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值