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.
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.
The first line of the input contains five integers n, a, b, c and d (1 ≤ n ≤ 100 000, 1 ≤ a, b, c, d ≤ n) — maximum possible value of an integer in the cell and four integers that Vasya remembers.
Print one integer — the number of distinct valid squares.
2 1 1 1 2
2
3 3 1 2 3
6
Below are all the possible paintings for the first sample.
In the second sample, only paintings displayed below satisfy all the rules.
B. Restoring Painting
自己画一个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; }
#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