Codeforces Round #353 (Div. 2) B. Restoring Painting __ map or set 、思维题

B. Restoring Painting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was stolen while he was on duty. He doesn't want to be fired, so he has to quickly restore the painting. He remembers some facts about it.

  • The painting is a square 3 × 3, each cell contains a single integer from 1 to n, and different cells may contain either different or equal integers.
  • The sum of integers in each of four squares 2 × 2 is equal to the sum of integers in the top left square 2 × 2.
  • Four elements abc and d are known and are located as shown on the picture below.
Codeforces Round #353 (Div. 2) B. Restoring Painting __ map or set 、思维题_第1张图片

Help Vasya find out the number of distinct squares the satisfy all the conditions above. Note, that this number may be equal to 0, meaning Vasya remembers something wrong.

Two squares are considered to be different, if there exists a cell that contains two different integers in different squares.

Input

The first line of the input contains five integers nabc and d (1 ≤ n ≤ 100 0001 ≤ a, b, c, d ≤ n) — maximum possible value of an integer in the cell and four integers that Vasya remembers.

Output

Print one integer — the number of distinct valid squares.

Examples
input
2 1 1 1 2
output
2
input
3 3 1 2 3
output
6
Note

Below are all the possible paintings for the first sample.Codeforces Round #353 (Div. 2) B. Restoring Painting __ map or set 、思维题_第2张图片

In the second sample, only paintings displayed below satisfy all the rules.Codeforces Round #353 (Div. 2) B. Restoring Painting __ map or set 、思维题_第3张图片Codeforces Round #353 (Div. 2) B. Restoring Painting __ map or set 、思维题_第4张图片



Source

B. Restoring Painting


My Solution

自己画一个3*3的方格图, 然后标上 a, b, c, d 然后发现左上角标上x, 中间标上y,然后剩余3个空格可以表示出来。

故可以O(n)的来做

扫一遍,过程中用ans[][][][]来表示那个状态。

最后得到不同的个数

然后n*ans.size()就好了,(n 表示中间的数字的可能情况总数为n), 然后注意可能溢出就好了

以及判断,一些不可能出现的情况, ☺☺ 样例给的很良心

     3

1         2

     3 

这个时候左上角不可能是1的

另外每个格子里必须是  1<= x <= n 的数

Solution 1 当时用4重map莽夫了一把,嘿嘿没有MLE map<int, map<int ,map<int, map<int, int> > > > ans;  这样比较耗内存吧

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
/*
struct q{
    int lu, ru, ld, rd;
};
*/
map<int, map<int ,map<int, map<int, int> > > > ans;

int main()
{
    int n, a, b, c, d;
    scanf("%d%d%d%d%d", &n, &a, &b, &c, &d);
    for(int i = 1; i <= n; i++){
    /*
        val.lu = i;
        val.ru = i + b - c;
        val.ld = i + a - d;
        val.rd = i + a + b - d - c;
        ans.insert(val);
    */
        if(i + b - c <= n && i + b - c > 0 && i + a - d <= n && i + a - d > 0 && i + a + b - d - c <= n && i + a + b - d - c > 0){
            ans[i][i + b - c][i + a - d][i + a + b - d - c]++;
        }

    }
    long long val = n;
    val = val*ans.size();
    cout<<val;
    return 0;
}



Solution 2 建一个结构体 q, 然后四个成员 ru(rights up), lu(lest up), ld(lest down) and rd(right down), 然后重载好< 就好了,一个map就行

#include <iostream>
#include <cstdio>
#include <set>
using namespace std;

struct q{
    int lu, ru, ld, rd;


};

bool operator < (const q& a, const q& b)
{
    if(a.lu != b.lu) return a.lu < b.lu;
    else{
        if(a.rd != b.rd) return a.ld < b.ld;
        else{
            if(a.rd != b.rd) return a.rd < b.rd;
            else return a.ru < b.ru;
        }
    }
    //要全部定义不然可能有被覆盖掉
}

set<q> ans;

int main()
{
    int n, a, b, c, d;
    q val;
    scanf("%d%d%d%d%d", &n, &a, &b, &c, &d);
    for(int i = 1; i <= n; i++){
        val.lu = i;
        val.ru = i + b - c;
        val.ld = i + a - d;
        val.rd = i + a + b - d - c;

        if(val.ru <= n && val.ru > 0 && val.ld <= n && val.ld > 0 && val.rd <= n && val.rd > 0){
            ans.insert(val);
        }

    }
    long long v = n;
    v = v*ans.size();
    cout<<v;
    return 0;
}


Thank you!



                                                                                                                                               ------from ProLights

                                                                                                                                               ------from  ProLights

你可能感兴趣的:(map,set,ACM,round,or,codeforces,思维题)