A Compatible Pair(CodeForces 934A)选择路径输出两个路径相等的图形

A Compatible Pair

Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.

Little Tommy has n lanterns and Big Banban has m lanterns. Tommy's lanterns have brightness a1, a2, ..., an, and Banban's have brightness b1, b2, ..., bm respectively.

Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns.

Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible.

You are asked to find the brightness of the chosen pair if both of them choose optimally.

Input

The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 50).

The second line contains n space-separated integers a1, a2, ..., an.

The third line contains m space-separated integers b1, b2, ..., bm.

All the integers range from  - 109 to 109.

Output

Print a single integer — the brightness of the chosen pair.

Examples

input

2 2
20 18
2 14

output

252

input

5 3
-1 0 1 2 3
-1 0 1

output

2

Description
渔托邦可以想象成一个 4行,奇数列的网格。 它有两个主要的村庄; 第一个位于左上角(1,1),在那里的人喜欢在右下角的池(4,n)捕鱼。 第二个村庄位于(4,1),人们喜欢在左下角(1,n)的鲑鱼池塘。
 
渔托邦的市长希望在城市中放置 k 个酒店,每个酒店都占用一个网格。 为了让人们可以从任何地方进入城市,不能将酒店放在边上。
 
如果这些单元格没有被酒店占用并且有一共同边,则一个人可以从一个单元移动到另一个单元。
 
你能帮助市长安排酒店,使从每个村庄到其池塘的最短路径的数量相等吗?
Input
第一行输入包含两个整数,n 和 k(3 ≤ n ≤ 99,0 ≤ k ≤ 2 ×(n - 2)),n是奇数。 n和k 代表城市的宽度,以及要建设的酒店数量 。

Output
如果可以以满足问题陈述的方式放置所有酒店,则输出“YES”,否则输出“NO”。
 
如果可能的话,输出另外 4 行描述城市的行,每行应该有 n 个字符,如果该单元格上有酒店,则字符是“#”,如果是空地,字符是“.” 。
Sample Input
Input
7 2
Output
YES
.......
.#.....
.#.....
.......
Input
5 3
Output
YES
.....
.###.
.....
.....

思路:

不存在NO的情况,都是YES;

所以如果K是偶数就先放左边,如果K是奇数就从中间开始放;

题意:

有一个城市有4行n列,n是奇数,有一个村庄在(1,1),村民的活动地点是(4,n);

有一个村庄在(4,1),村民的活动地点是(1,n);

现在要修建k个宾馆,不能修建在边界上,问能否给出一种安排方案使得两个村庄的村民到他们各自的活动地点的最短路的条数相等。

思路:

无论n和k是多少,都可以构建出合理的方案,所以全是YES。

如果k为偶数,那么就上下对称,这个比较好构造;当k为奇数,我采用的是首先把第二排从中间开始向两边填满,然后第三排则是从中间一格的两边开始填。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
char a[5][105];
int main()
{
    int n,k,s;
    while(~scanf("%d%d",&n,&k))
    {
        memset(a,0,sizeof(a));
        printf("YES\n");
        if(k%2==0)//如果是偶数的话,进行上下增加,容易构造
        {
            for(int i=2; i<=k/2+1; i++)
            {
                a[2][i]='#';
                a[3][i]='#';
            }
        }
        else   //首先把第二排从中间开始向两边填满,然后第三排则是从中间一格的两边开始填满
        {
            if(k<=n-2)   //首先把第二排从中间开始向两边填满
            {
                int p=n/2+1;
                int l=1,r=0;
                for(int i=1; i<=k; i++)
                {
                    if(i%2==1)
                    {
                        a[2][p+r]='#';
                        r++;
                    }
                    else
                    {
                        a[2][p-l]='#';
                        l++;
                    }
                }
            }
            else  //第三排则是从中间一格的两边开始填满
            {
                for(int i=2; i<=n-1; i++)
                {
                    a[2][i]='#';//把第二行的酒店数全部打出
                }
                k=k-n+2;//求出剩下的要建造的酒店数
                int l=1,r=2;
                for(int i=1; i<=k; i++)
                {
                    if(i%2==1)
                    {
                        a[3][r]='#';//从左边开始保存数据
                        r++;//保存一次进行加一
                    }
                    else
                    {
                        a[3][n-l]='#';//从右边开始保存数据
                        l++;//保存一次进行加一
                    }
                }
            }
        }
        for(int i=1; i<=4; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(a[i][j]!='#')
                    a[i][j]='.';
                else
                    s++;
                printf("%c",a[i][j]);
            }
            printf("\n");
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38984851/article/details/81068230