zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    整理打印PI值
    使用自连接、for xml path('')和stuff合并显示多行数据到一行中(转)
    ThinkPHP+jQuery EasyUI Datagrid查询数据的简单处理
    ThinkPHP学习(三)
    Apache+PHP配置PATHINFO的一个小问题
    ThinkPHP学习(二)
    接口签名工具类
    RSA加密和解密工具类
    kafka项目中踩到的一个坑(客户端和服务器端版本不一致问题)
    自己实现字符串转整数(不使用JDK的字符串转整数的方法)
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/10366506.html
Copyright © 2011-2022 走看看