Editorial for Lâu đài cát


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

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

const fi='';
      fo='';
      maxn=25000;
type ar=array[1..maxn] of longint;
var n,x,y,re:longint;
    a,b:ar;

procedure rf;
var i:longint;
begin
     assign(input,fi);
     reset(input);
     read(n,x,y);
     for i:=1 to n do read(a[i],b[i]);
     close(input);
end;

procedure sort(var a:ar;l,r:longint);
var x,y,i,j:longint;
begin
     i:=l; j:=r; x:=a[(i+j) div 2];
     repeat
           while a[i]<x do i:=i+1;
           while a[j]>x do j:=j-1;
           if i<=j then
           begin
                y:=a[i]; a[i]:=a[j]; a[j]:=y;
                i:=i+1; j:=j-1;
           end;
     until i>j;
     if i<r then sort(a,i,r);
     if l<j then sort(a,l,j);
end;

procedure pr;
var i:longint;
begin
     re:=0;
     sort(a,1,n);
     sort(b,1,n);
     for i:=1 to n do
         if a[i]<b[i] then re:=re+x*(b[i]-a[i])
         else re:=re+y*(a[i]-b[i]);
end;

procedure wf;
begin
     assign(output,fo);
     rewrite(output);
     write(re);
     close(output);
end;

begin
     rf;
     pr;
     wf;
end.

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;
typedef long long LL;

#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(); i != _e; ++i)
#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 25000+5

int a[N], b[N], n;
LL x, y;

int main() {
    scanf("%d%lld%lld", &n, &x, &y);
    rep(i,n) scanf("%d%d",a+i,b+i);
    sort(a,a+n); sort(b,b+n);
    LL res = 0;
    rep(i,n)
        if(a[i] > b[i]) res += y*(a[i]-b[i]);
        else res += x*(b[i]-a[i]);
    printf("%lld\n", res);
    return 0;
}

Code mẫu của ladpro98

program ctnews;
uses    math;
var     n,x,y:longint;
        a:array[1..2,1..25555] of longint;
        i:longint;
        res:int64;
procedure input;
var     f:text;
        i:longint;
begin
        assign(f,'');
        reset(f);
        readln(f,n,x,y);
        for i:=1 to n do
        readln(f,a[1,i],a[2,i]);
        close(f);
end;

procedure swap(k,i,j:longint);
var     t:longint;
begin
        t:=a[k,i];
        a[k,i]:=a[k,j];
        a[k,j]:=t;
end;

procedure sort(k,l,r:longint);
var     p,i,j:longint;
begin
        if l>=r then exit;
        p:=a[k,random(r-l+1)+l];
        i:=l;j:=r;
        repeat
                while a[k,i]<p do inc(i);
                while a[k,j]>p do dec(j);
                if i<=j then
                begin
                        if i<j then swap(k,i,j);
                        inc(i);dec(j);
                end;
        until i>j;
        sort(k,l,j);sort(k,i,r);
end;

begin
        input;
        sort(1,1,n);
        sort(2,1,n);
        for i:=1 to n do
        begin
                if a[1,i]>a[2,i] then
                res:=res+(a[1,i]-a[2,i])*y
                else
                res:=res+(a[2,i]-a[1,i])*x;
        end;
        write(res);
end.

Code mẫu của RR

#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAXN 25111
#define FOR(i,a,b) for(long i=a; i<=b; i++)

using namespace std;

long a[MAXN],b[MAXN],x,y,n;

int main() {
    scanf("%ld %ld %ld",&n,&x,&y);
    FOR(i,0,n-1) scanf("%ld %ld",&a[i],&b[i]);
    sort(a,a+n);
    sort(b,b+n);
    long long res=0;
    FOR(i,0,n-1)
      if (a[i]<b[i]) res+=(long long) (b[i]-a[i])*x;
      else res+=(long long) (a[i]-b[i])*y;
    cout<<res;
    return 0;
}

Code mẫu của hieult

#include <stdio.h>
//#include <conio.h>
void Quicksort(long A[],long lower,long upper)
{
        long x = A[(lower + upper) / 2];
        long i = lower;
        long j = upper;
        do{
                while(A[i] < x)
                        i ++;
                while (A[j] > x)
                        j --;
                if (i <= j)
                {
                     long tg=A[i];
                     A[i]=A[j];
                     A[j]=tg;   
                        i ++;
                        j --;
                }
        }while(i <= j);
        if (j > lower)
                Quicksort(A, lower, j);
        if (i < upper)
                Quicksort(A, i, upper);
}  
main()
{
long n,x,y,a[25002],b[25002];
long long KQ=0;
scanf("%ld %ld %ld",&n,&x,&y);
for(long i=1;i<=n;i++)
  scanf("%ld %ld",&a[i],&b[i]);
Quicksort(a,1,n);
Quicksort(b,1,n);
for(long i=1;i<=n;i++)
  {
  if(a[i]>b[i])
    KQ=KQ+(a[i]-b[i])*y;
  else KQ=KQ+(b[i]-a[i])*x;
  }
printf("%lld",KQ);
//getch();
}

Code mẫu của ll931110

#include <iostream>
#include <cstdlib>
#define MAXN 25000
using namespace std;

int compare(const void* u, const void* v)
{
    int* x1 = (int*) u;
    int* y1 = (int*) v;

    if (*x1 > *y1) return -1;
    else if (*x1 == *y1) return 0;
    else return 1;
}

int a[MAXN],b[MAXN],n,x,y;
long long cost;

int main()
{
    int i;

    //freopen("ctnews.inp","r",stdin);
    //freopen("c1.out","w",stdout);

    scanf("%d%d%d", &n, &x, &y);
    for (i = 0; i < n; i++) scanf("%d%d", &a[i], &b[i]);

    qsort(a, n, sizeof(int), compare);
    qsort(b, n, sizeof(int), compare);

    cost = 0;
    for (i = 0; i < n; i++)
      if (a[i] > b[i]) cost += y * (a[i] - b[i]);
      else if (a[i] < b[i]) cost += x * (b[i] - a[i]);

    printf("%lld", cost);
}

Code mẫu của khuc_tuan

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

using namespace std;

#define Rep(i,n) for(int i=0,lll=(n);i<lll;++i)
#define For(i,a,b) for(int i=(a),lll=(b);i<=lll;++i)
#define Ford(i,a,b) for(int i=(a),lll=(b);i>=lll;--i)
#define pb push_back
#define MP make_pair
#define fi first
#define se second
#define nextint __nextint()

inline int __nextint() { int x; scanf("%d", &x); return x; }

int n = nextint;
long long x = nextint, y = nextint;
int a[25000], b[25000];

int main() {
    Rep(i,n) scanf("%d%d", a+i, b+i);
    sort( a, a + n);
    sort( b, b + n);
    long long res = 0;
    Rep(i,n) {
        if(a[i] < b[i]) res += x * (b[i]-a[i]);
        else res += y * (a[i] - b[i]);
    }
    cout << res << endl;
//  system("pause");
    return 0;
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.