Hướng dẫn giải của Cách nhiệ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 flashmt
#include<iostream> int main() { int n,i,j,x,d[10010],a[100010]; long long re; re=0; scanf("%d",&n); for (i=1;i<=10010;i++) d[i]=0; for (i=1;i<=n;i++) { scanf("%d",&x); re+=x; d[x]++; } n=0; for (i=1;i<=10000;i++) if (d[i]>0) for (j=1;j<=d[i];j++) { n++; a[n]=i; } for (i=1;i<n-i+1;i++) re+=a[n-i+1]-a[i]; printf("%lld\n",re); return 0; }
Code mẫu của happyboy99x
#include <algorithm> #include <bitset> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<vii> vvii; typedef vector<int> vi; typedef vector<vi> vvi; #define sz(a) int((a).size()) #define fi first #define se second #define pb push_back #define mp make_pair #define all(c) (c).begin(), (c).end() #define tr(c,i) for(typeof((c).begin()) i = (c).begin(), _e = (c).end(); it != _e; ++it) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(all(c),x) != (c).end()) #define rep(i,n) for(int i = 0, _n = (n); i < _n; ++i) #define repd(i,n) for(int i = (n)-1; i >= 0; --i ) #define fo(i,a,b) for(int i = (a), _b = (b); i <= _b; ++i) #define fod(i,a,b) for(int i = (a), _b = (b); i >= _b; --i) #define INF 1000000000 #define N 100005 int n, a[N]; int main() { scanf( "%d", &n ); rep(i,n) scanf( "%d", a+i ); sort(a, a+n); int res = 0; rep(i, n) res += a[i]; for( int i = 0, j = n - 1; i < j; ++i, --j ) res += a[j] - a[i]; printf( "%d\n", res ); return 0; }
Code mẫu của ladpro98
program insul; uses math; const fi=''; maxN=100001; var a:array[1..maxN] of longint; n,res:longint; procedure input; var inp:text; i:longint; begin assign(inp,fi); reset(inp); readln(inp,n); for i:=1 to n do readln(inp,a[i]); close(inp); end; 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 i,j,pivot: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; procedure process; var i:longint; begin res:=0; for i:=1 to n do inc(res,a[i]); for i:=1 to n div 2 do inc(res,a[n-i+1]-a[i]); end; begin input; sort(1,n); process; write(res); end.
Code mẫu của hieult
#include <stdio.h> //#include <conio.h> int partition(int array[], int top, int bottom); void QuickSort(int num[], int top, int bottom) { int middle; if (top < bottom) { middle = partition(num, top, bottom); QuickSort(num, top, middle); QuickSort(num, middle+1, bottom); } return; } int partition(int array[], int top, int bottom) { int x = array[top]; int i = top - 1; int j = bottom + 1; int temp; do { do { j --; }while (x >array[j]); do { i++; } while (x <array[i]); if (i < j) { temp = array[i]; array[i] = array[j]; array[j] = temp; } }while (i < j); return j; } int main() { int n,a[100001];long long KQ = 0; scanf("%d",&n); for(int i = 1;i<=n;i++) { scanf("%d",&a[i]); KQ = KQ + a[i]; } QuickSort(a,1,n); for(int i = 1;i<=n/2;i++) KQ = KQ + a[i]-a[n+1-i]; printf("%lld\n",KQ); //getch(); }
Code mẫu của ll931110
#include <algorithm> #include <iostream> using namespace std; int a[100010],n; int main() { // freopen("insul.in","r",stdin); // freopen("insul.ou","w",stdout); scanf("%d", &n); int sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; }; sort(a,a + n); for (int i = n - 1; i >= 0; i--) { int j = n - 1 - i; if (i <= j) break; sum += (a[i] - a[j]); }; printf("%d\n", sum); };
Bình luận