Hướng dẫn giải của Trò chơi xếp hình chữ nhật
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của happyboy99x
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { ios::sync_with_stdio(false); int nTest; cin >> nTest; for(int test = 0; test < nTest; ++test) { int n; cin >> n; vector<int> a (n); for(int i = 0; i < n; ++i) cin >> a[i]; sort(a.rbegin(), a.rend()); bool found = false; for(int i = 0, p = -1; i < (int) a.size(); ) { int j = i; while(j < (int) a.size() && a[i] == a[j]) ++j; if(j - i >= 2) { if(p == -1) { if(j - i >= 4) { cout << 1LL * a[i] * a[i] << '\n'; found = true; break; } else p = a[i]; } else { cout << 1LL * p * a[i] << '\n'; found = true; break; } } i = j; } if(!found) cout << -1 << '\n'; } return 0; }
Code mẫu của ladpro98
const fi=''; maxn=333333; var k,kk,n,i:longint; a:array[1..maxn] of longint; res:int64; inp:text; procedure swap(i,j:longint); var t:longint; begin t:=a[i]; a[i]:=a[j]; a[j]:=t; end; procedure sort(l,r:longint); var pivot,i,j:longint; begin if l>=r then exit; pivot:=a[random(r-l+1)+l]; i:=l;j:=r; repeat; while a[i]>pivot do inc(i); while a[j]<pivot do dec(j); if i<=j then begin if i<j then swap(i,j); inc(i); dec(j); end; until i>j; sort(l,j);sort(i,r); end; begin assign(inp,fi);reset(inp); readln(inp,k); for kk:=1 to k do begin readln(inp,n); res:=-1; for i:=1 to n do read(inp,a[i]); sort(1,n); i:=1; while i<n do begin if (a[i]=a[i+1]) then begin if res=-1 then res:=-a[i] else begin res:=-res*a[i]; break; end; inc(i); end; inc(i); end; if res<0 then res:=-1; writeln(res); end; end.
Code mẫu của skyvn97
#include<bits/stdc++.h> #define MAX 100100 #define FOR(i,a,b) for (int i=(a);i<=(b);i=i+1) #define REP(i,n) for (int i=0;i<(n);i=i+1) #define FORE(i,v) for (__typeof((v).begin()) i=(v).begin();i!=(v).end();i++) #define fi first #define se second using namespace std; typedef pair<int,int> ii; void process(void) { int n; map<int,int> mp; vector<ii> v; scanf("%d",&n); REP(zz,n) { int t; scanf("%d",&t); mp[t]++; } FORE(it,mp) v.push_back(*it); reverse(v.begin(),v.end()); //FORE(it,v) printf("%d %d\n",it->fi,it->se); vector<int> res; FORE(it,v) while (res.size()<2 && it->se>=2) { res.push_back(it->fi); it->se-=2; } if (res.size()<2) cout<<-1<<"\n"; else cout<<1LL*res[0]*res[1]<<"\n"; } int main(void) { int t; scanf("%d",&t); REP(zz,t) process(); return 0; }
Bình luận