• NOIP模拟 --切木板


    题目

    有一个 m×n 的矩形木板。你需要把这个木板切成 1×1 的小方块,也就是竖着切 n-1 刀、横着切 m-1 刀。横着切第 i 个位置的权值为 xi ,竖着切第 j 个位置的权值为 yj 。切某一刀时的费用为切这一刀的权值乘上切过的块数。
    请你安排切的顺序使得所有费用之和最小。
    输入格式
    第一行两个数 m,n 。
    接下来一行 m-1 个整数 x1,x2,…,xm-1 。
    接下来一行 n-1 个整数 y1,y2,…,yn-1 。
    输出格式
    输出一个数,表示最小的费用之和 mod 109 + 7 。
    样例数据 1
    输入
    6 4
    2 1 3 1 4
    4 1 2
    输出
    42
    备注
    【数据规模与约定】
    对于 30% 的数据, m + n≤10 。
    对于 60% 的数据, m,n≤500 。
    对于 100% 的数据, 2≤m,n≤10^6 ;0≤xi,yj≤10^9 。
    【提醒】
    建议本题加读入优化。

    也是一个很简单的贪心,可以证明先切权值大的刀更优。。。。然后?就没有然后了

    #include<bits/stdc++.h> 
    using namespace std;
    #define ll long long
    ll mod=1e9+7;
    inline ll read(){
        char ch=getchar();
        ll  res=0;
        while(!isdigit(ch)) ch=getchar();
        while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
        return res;
    }
    int n,m,numa,numb;
    ll tota,totb,a[1000006],b[1000006],ans;
    inline bool  comp(int a,int b)
    {
        return a>b;
    }
    int main(){
    
        scanf("%d%d",&m,&n);
        for(int i=1;i<m;i++)
        {
            a[i]=read();
            tota+=a[i];
        }
        for(int i=1;i<n;i++)
        {
            b[i]=read();
            totb+=b[i];
        }
        sort(a+1,a+m+1,comp);
        sort(b+1,b+n+1,comp);
        int pa=1,pb=1;
        numa=1,numb=1;
        while(tota!=0||totb!=0)
        {
            if(a[pa]>b[pb])
            {
                tota-=a[pa],numa++;
                ans+=a[pa]*numb;
                if(ans>=mod) ans%=mod;
                pa++;
            }
            else{
                totb-=b[pb],numb++;
                ans+=b[pb]*numa;
                pb++;
                if(ans>=mod) ans%=mod;
            }
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    Python:字典
    Linux基础:dirname命令总结
    (三)封装与类
    (二)Java编程基础
    Ubuntu18.04 安装QQ、Tim、微信与win无差异
    (一)JDK安装和使用eclipse输出hello world
    Java复习目录
    (八)MySQL事务、视图、变量、存储过程、函数、流程控制结构
    (七)MySQL常见的数据类型、约束和标识列
    (六)MySQL数据、库、表的管理
  • 原文地址:https://www.cnblogs.com/forever-/p/9736074.html
Copyright © 2020-2023  润新知