Hướng dẫn giải của ACM
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<numeric> #include<vector> using namespace std; int main() { int n; cin >> n; vector<int> v; int res = 0; for(int i = 0, a, b; i < 2 * n; ++i) { cin >> a >> b; res += a; v.push_back(b - a); } sort(v.begin(), v.end()); cout << res + accumulate(v.begin(), v.begin() + n, 0) << '\n'; return 0; }
Code mẫu của ladpro98
program acmnb; uses math; const fi=''; maxN=800004; type e=record x,y,s:longint; end; var a:array[1..maxN] of e; n:longint; procedure input; var f:text; i:longint; begin assign(f,fi); reset(f); readln(f,n); for i:=1 to n shl 1 do begin readln(f,a[i].x,a[i].y); a[i].s:=a[i].x-a[i].y; end; close(f); end; procedure swap(i,j:longint); var t:e; begin t:=a[i]; a[i]:=a[j]; a[j]:=t; end; procedure sort(l,r:longint); var p,i,j:longint; begin if l>=r then exit; p:=a[random(r-l+1)+l].s; i:=l;j:=r; repeat while a[i].s<p do inc(i); while a[j].s>p 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; procedure cal; var i,res:longint; begin res:=0; for i:=1 to n do inc(res,a[i].x); for i:=n+1 to n shl 1 do inc(res,a[i].y); write(res); end; begin input; sort(1,n shl 1); cal; end.
Code mẫu của skyvn97
#include<algorithm> #include<cstdio> #define MAX 800800 #define FOR(i,a,b) for (int i=(a);i<=(b);i=i+1) #define fi first #define se second using namespace std; typedef pair<int,int> ii; int n; ii a[MAX]; bool cmp(const ii &a,const ii &b) { return (a.fi-a.se<b.fi-b.se); } void init(void) { scanf("%d",&n); FOR(i,1,2*n) { scanf("%d",&a[i].fi); scanf("%d",&a[i].se); } sort(a+1,a+2*n+1,cmp); } void process(void) { int res=0; FOR(i,1,n) res+=a[i].fi; FOR(i,n+1,2*n) res+=a[i].se; printf("%d",res); } int main(void) { //freopen("ACM.INP","r",stdin); //freopen("ACM.OUT","w",stdout); init(); process(); return 0; }
Bình luận