【Henu ACM Round#18 C】Ilya and Sticks
阿新 • • 發佈:2018-01-29
local return std codeforce clas ble fin back tdi
【鏈接】 我是鏈接,點我呀:)
【題意】
在這裏輸入題意
【題解】
用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;
}
【Henu ACM Round#18 C】Ilya and Sticks