题目链接:点击打开链接
给出n个棒子的长度, 每个棒子可以变为自己的len - 1, 问可以组成题目中要求的矩形的面子最大是多少.
将棒子排序, 由大到小遍历棒子, 由于每个棒子可以变成自己的len - 1, 若相邻棒子差值为1或0, 则四边构成一个矩形, 累加得到答案.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 5;
int n;
ll x, ans;
int main(int argc, char const *argv[])
{
cin >> n;
vector<int> v(n);
for(int i = 0; i < n; ++i)
cin >> v[i];
sort(v.begin(), v.end());
for(int i = n - 2; i >= 0; --i)
if(v[i + 1] - v[i] < 2) {
if(x) {
ans += x * v[i];
x = 0;
}
else x = v[i];
i--;
}
cout << ans << endl;
return 0;
}