【Henu ACM Round#18 C】Ilya and Sticks

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

用cnt[i]记录数字i出现的次数就好。 然后i从1e6逆序到1 如果cnt[i+1]和cnt[i]>0同时成立的话。 那么得到一条边。加入到vector中。

然后

如果cnt[i]>1 则cnt[i]-=2 加入i到vector中,直到cnt[i]<=1为止。

注意这两个判断的先后顺序。
否则
5 4 4 3会被认为无解。

vector的大小如果大于等于2了。
就说明找到一个矩形。

【代码】

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e6;

int n,cnt[N+10];
ll ans = 0;

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    #ifdef LOCAL_DEFINE
        freopen("rush.txt","r",stdin);
    #endif
    cin >> n;
    for (int i = 1;i <= n;i++) {
        int x;
        cin >> x;
        cnt[x]++;
    }
    vector<int> v;v.clear();
    for (int i = N;i >= 1;i--){

        if (cnt[i+1]>0 && cnt[i]>0){
            v.push_back(i);
            if ((int)v.size()>1) {
                ans+=1LL*v[0]*v[1];
                v.clear();
            }
            cnt[i+1]--;cnt[i]--;
        }

        while (cnt[i]>1){
            cnt[i]-=2;
            v.push_back(i);
            if ((int)v.size()>1) {
                ans+=1LL*v[0]*v[1];
                v.clear();
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/8376561.html