poj 1736

Block Town
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 667 Accepted: 257

Description

Children like playing with blocks (cube wooden bricks). They usually build high towers, but small Johny dreams of different plans. He is going to build a big town. His daddy has bought him a rectangular table; its width is K blocks and its length is L blocks exactly. Johny decided to project a plan of such a town before he starts building the town itself. He has drawn a square-shaped network on the table consisting of KxL squares. He wants to place the towers consisting of one or more blocks on some of the squares of the network drawn; the remaining squares will be empty. Because of the table being so large,Johny is not going to plan exactly for every square how many blocks he will put on it. He only wants to decide about front and right sight shapes of his town. He drew these two views (two-dimensional projections of the planned town) on a paper. You can see an example of these drawings and the adequate town made of wooden bricks in the pictures:


side view        front view      maximal town         minimal town (both front and back view)

Johny's daddy is afraid they don't have enough blocks to finish building Johny's planned town. You are asked for writing a program to compute the minimal and maximal amount of blocks with which a town corresponding to Johny's plans can be built. Moreover the program can decide about the possibility of building a town satisfying the views.

Input

The first line of input contains two positive integers K, L -- the width and the length of the table (expressed as numbers of bricks). Neither the width nor the length of the table is greater than 100000 bricks. The following lines of the input file contain the description of the front view of the town. The description consists of a series of heights of visible buildings on each square from the left to the right (the height is measured by the number of the blocks, too). There is only one number on each line, i.e. the number of the lines with the front view description of the town equals K -- the width of the table. Similarly the next L lines of the input file contain the right sight view of the town. The heights of the wooden block towers are now specified from the front line to the back line. You may suppose there is no building in the town with height exceeding 5000 blocks. The maximal number of blocks needed for building the entire town does not exceed 2000000000.

Output

Output contains only one line. If it is not possible to build a town with the front and right sight views given, only a text `No solution.' is written there. In the other case two numbers will be written on the line and separated by a single space. The first one is the minimal and the second one is the maximal amount of blocks small Johny can use to build his town in accordance with his project.

Sample Input

4 3
1
3
4
2
1
4
2

Sample Output

10 21

Source

PS:在湖大的第一题,尽管是水题还是wa了。。。“No solution.”啊 委屈
/*
Code for poj 1736
By Pira 2011.7.22
*/
#include<cstdio>
#include<cstring>
using namespace std;
int i,j,k,l,num,ans1,ans2,m1,m2,a[5555],b[5555],c[5555];
int main()
{
    while(scanf("%d%d",&k,&l)!=-1)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        ans1=ans2=m1=m2=0;
        for(i=0;i<k;++i)
        {
            scanf("%d",&num);
            ++a[num];
            ans1+=num;
            ans2+=num*l;
            if(num>m1)m1=num;
        }
        for(i=5000;i>=0;--i)c[i]=c[i+1]+a[i+1],b[i]=b[i+1]+a[i+1]*(i+1);
        for(i=5000;i>=0;--i)b[i]=b[i]-c[i]*i;
        for(i=0;i<l;++i)
        {
            scanf("%d",&num);
            if(a[num])--a[num];
            else ans1+=num;
            ans2-=b[num];
            if(num>m2)m2=num;
        }
        if(m1==m2)printf("%d %d\n",ans1,ans2);
        else printf("No solution.\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值