zoukankan      html  css  js  c++  java
  • 隔壁-贪心

    背景: 
    Cax军训了,隔壁某世界二流学校开始搞事情,派遣cax军训观光团前来扰乱士气,经过严密筹划,cax决定为民除害,直接用拖拉机铲平隔壁学校。 
    题目描述: 
    隔壁学校地形图可以通过一个高度矩阵表示,矩阵中每一个位置都有一个数0≤h(i,j) ≤10^5表示这个坐标的海拔,我们姑且称之为海拔图,容易发现,我们可以通过这个矩阵轻松地算出隔壁学校的主视图,左视图,相反的,我们却不能通过主视图和左视图唯一确定海拔图,现在问题来了,已知主视图,左视图,我们需要知道铲平隔壁的代价上限与下限(即可能的体积最大值与最小值) 
    输入描述: 
    第一行两个数n,m,分别表示海拔图的长和宽 
    第二行n个数,描述了主视图每一个位置的高度 
    第二行m个数,描述了左视图每一个位置的高度 
    输出描述: 
    一行两个数,分别表示代价最小值与最大值 
    样例输入: 
    2 2 
    1 1 
    1 1 
    样例输出: 
    2 4 
    数据范围: 
    对于10%的数据,满足n=m=1 
    对于另外10%的数据,满足n=m=2 
    对于另外20%的数据,满足1≤n,m≤3且0≤h(i,j) ≤3 
    对于100%的数据,满足1≤n,m≤1000且0≤h(i,j) ≤1000

    思路:

      先想最小值:我们让最高的值一定存在并且让它们横向和纵向的最大值尽可能重合在一个位置,把这些和加起来(其余位置是0)。

      最大值的话,在满足不改变最大值的情况下让每个位置尽可能取最大的值(取n*m次min)

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int n,m;
    int all,mall;
    int x[1200],y[1200],tx[1200],ty[1200];
    bool  comp(const  int & x,const int &y)
    {
        return x > y;
    }
    int main()
    {
        freopen("neighbor.in","r",stdin);
        freopen("neighbor.out","w",stdout);
        scanf("%d%d",&n,&m);
        
        for(int i=1;i<=n;i++)
            scanf("%d",&x[i]),tx[i]=x[i];
        for(int j=1;j<=m;j++)
            scanf("%d",&y[j]),ty[j]=y[j];
        sort(x+1,x+1+n,comp);sort(y+1,y+1+m,comp);    
        int xx=1,yy=1;
        while(xx<=n&&yy<=m)
        {
            if(x[xx]==y[yy])    all+=x[xx],xx++,yy++;
            else
            {
                if(x[xx]>y[yy])            
                    all+=x[xx++];
                else all+=y[yy++];
            }
        }
        while(xx<n+1)    all+=x[xx++];
        while(yy<m+1)    all+=y[yy++];
        
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            mall+=min(tx[i],ty[j]);
        printf("%d %d",all,mall);
        return 0;
    } 

    样例

    输入

    4 3
    4 2 1 1
    4 3 1

    输出

    11 19

  • 相关阅读:
    (转)在WPF中自定义控件 CustomControl (下)注意TemplatePartAttribute
    [STAThread]的含义
    Exception of Storyboard in controlTemplate,can't use binding or dynamic resource
    What is the difference between CollectionView and CollectionViewSource?
    EssentialWPF_chapter6_Data
    WPF 调试方法, WPF Debug
    System.Windows.Markup.ContentPropertyAttribute
    Layout相关
    When use registerReadonly
    注意:匿名事件处理函数
  • 原文地址:https://www.cnblogs.com/CLGYPYJ/p/7562131.html
Copyright © 2011-2022 走看看