Editorial for VOI 08 Bài 1 - Trò chơi với dãy số


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

uses math;
var i,j,n,re:longint;
    a,b:array[1..100000] of longint;

procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
        i:=l; j:=r; x:=a[(i+j) shr 1];
        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(i,r);
        if l<j then sort(l,j);
end;

function bs(x:longint):longint;
var l,r,m,i:longint;
begin
        l:=1; r:=n;
        while l<=r do
        begin
                m:=(l+r) shr 1;
                if a[m]=x then break;
                if a[m]<x then l:=m+1
                else r:=m-1;
        end;
        r:=maxlongint;
        for i:=m-1 to m+1 do
            if (i>0) and (i<=n) then
               r:=min(r,abs(a[i]-x));
        bs:=r;
end;

begin
        read(n);
        for i:=1 to n do read(a[i]);
        for i:=1 to n do read(b[i]);
        sort(1,n);
        re:=abs(a[1]+b[1]);
        for i:=1 to n do
        begin
                j:=bs(-b[i]);
                if j<re then re:=j;
                if re=0 then break;
        end;
        writeln(re);
end.

Code mẫu của happyboy99x

#include <cstdio>
#include <algorithm>
#include <climits>
using namespace std;

#define SZ 100000+5
#define REP(i, n) for( int i = 0, _n = (n); i < _n; ++i )
int a[SZ], b[SZ], n;

int abs( int x ) { return x > 0 ? x : (-x); }

int bsearch( int * a, int x ) {
    if ( a[0] > x ) return a[0];
    if ( a[n-1] < x ) return a[n-1];
    int l = 0, h = n-1;
    while( l <= h ) {
        int mid = (l+h)/2;
        if ( a[mid] == x ) return a[mid];
        else if ( a[mid] < x ) l = mid + 1;
        else h = mid - 1;
    }
    if ( abs(a[l] - x) < abs(a[h] - x) ) return a[l];
    return a[h];
}

int main() {
    scanf( "%d", &n );
    REP(i, n) scanf( "%d", a+i ); REP(i, n) scanf( "%d", b+i );
    sort(b, b+n); int min = INT_MAX;
    REP(i, n) {
        int x = bsearch(b, -a[i]);
        int x2 = abs(a[i] + x);
        if ( x2 == 0 ) { puts("0"); return 0; }
        if ( x2 < min ) min = x2;
    }
    printf( "%d\n", min );
    return 0;
}

Code mẫu của ladpro98

program NKSGAME;
uses    math;
const   maxn=200005;
        fi='';
type    e=record
                v,p:longint;
        end;
var     a:array[1..maxn] of e;
        n,i,x,pa,pb,res:longint;
        inp:text;

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     pivot,i,j:longint;
begin
        if l>=r then exit;
        pivot:=a[random(r-l+1)+l].v;
        i:=l;j:=r;
        repeat;
                while a[i].v<pivot do inc(i);
                while a[j].v>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,n);
        for i:=1 to n do begin
                read(inp,x);
                a[i].v:=x;
                a[i].p:=1;
        end;
        for i:=n+1 to 2*n do begin
                read(inp,x);
                a[i].v:=-x;
                a[i].p:=2;
        end;
        n:=2*n;res:=high(longint);
        sort(1,n);
        for i:=1 to n do begin
                if (a[i].p=2) then begin
                        if pa>0 then
                        res:=min(res,abs(a[i].v-a[pa].v));
                        pb:=i;
                end;
                if (a[i].p=1) then begin
                        if pb>0 then
                        res:=min(res,abs(a[i].v-a[pb].v));
                        pa:=i;
                end;
        end;
        write(res);
end.

Code mẫu của RR

uses math;
const
  MAXN=100111;

var
  n:longint;
  a:array[0..1,1..MAXN] of longint;
  res,i,j:longint;

procedure swap(var a,b:longint);
    var
      tmp:longint;
    begin
      tmp:=a; a:=b; b:=tmp;
    end;

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

begin
  read(n);
  for i:=1 to n do read(a[0,i]);
  for i:=1 to n do read(a[1,i]);

  sort(0,1,n);
  sort(1,1,n);

  i:=1; j:=n; res:=abs(a[0,i]+a[1,j]);
  while (i<n) or (j>1) do
    begin
      if (i=n) then dec(j)
      else if (j=1) then inc(i)
      else if abs(a[0,i+1]+a[1,j]) < abs(a[0,i]+a[1,j-1]) then inc(i)
      else dec(j);

      res:=min(res,abs(a[0,i]+a[1,j]));
    end;
  writeln(res);
end.

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,a[100001],b[100001],Min=2000000000,x,y;      
scanf("%ld",&n);
  for(long i=1;i<=n;i++)
    scanf("%ld",&a[i]);
  for(long i=1;i<=n;i++)
    scanf("%ld",&b[i]);
quickSort(a,1,n);
quickSort(b,1,n);
x=1;y=n;
do
  {
  if(x==n&&a[x]+b[y]<0)
     {
     if(-(a[x]+b[y])<Min)
       Min=-(a[x]+b[y]);                        
     break;
     }
  else if(y==1&&a[x]+b[y]>0)
     {
     if(a[x]+b[y]<Min)
       Min=a[x]+b[y];                         
     break;
     }     
  else if(a[x]+b[y]<0)
     {
     if(-(a[x]+b[y])<Min)
       Min=-(a[x]+b[y]);  
     x++;
     }
  else if(a[x]+b[y]>0)
     {
     if(a[x]+b[y]<Min)
       Min=a[x]+b[y];  
     y--;
     }                                       
  else if(a[x]+b[y]==0)
     {
     Min=0;
     break;
     }
  }while(1);
printf("%ld",Min);
//getch();
}

Code mẫu của ll931110

Program NKSGAME;
        Const
                input  = '';
                output = '';
        Type
                arr = array[0..10000000] of longint;
        Var
                b,c: arr;
                  n: longint;

Procedure init;
          Var
                f: text;
                i: longint;
          Begin
                Assign(f, input);
                        Reset(f);
                        Readln(f, n);
                        For i:= 1 to n do read(f, b[i]);
                        For i:= 1 to n do read(f, c[i]);
                Close(f);
          End;

Procedure q1;

          Procedure partition(l,h: longint);
                    Var
                        i,j,p,x: longint;
                    Begin
                        If l >= h then exit;
                        p:= b[random(h - l + 1) + l];

                        i:= l;          j:= h;

                        Repeat
                                While b[i] < p do inc(i);
                                While b[j] > p do dec(j);

                                If i <= j then
                                        Begin
                                                If i < j then
                                                        Begin
                                                                x:= b[i];
                                                                b[i]:= b[j];
                                                                b[j]:= x;
                                                        End;
                                                inc(i);
                                                dec(j);
                                        End;
                        Until i > j;

                        partition(l,j);
                        partition(i,h);
                    End;
          Begin
                partition(1,n);
          End;

Procedure q2;

          Procedure partition(l,h: longint);
                    Var
                        i,j,p,x: longint;
                    Begin
                        If l >= h then exit;
                        p:= c[random(h - l + 1) + l];

                        i:= l;          j:= h;

                        Repeat
                                While c[i] < p do inc(i);
                                While c[j] > p do dec(j);

                                If i <= j then
                                        Begin
                                                If i < j then
                                                        Begin
                                                                x:= c[i];
                                                                c[i]:= c[j];
                                                                c[j]:= x;
                                                        End;
                                                inc(i);
                                                dec(j);
                                        End;
                        Until i > j;

                        partition(l,j);
                        partition(i,h);
                    End;
          Begin
                partition(1,n);
          End;

Procedure solve;
          Var
                      f: text;
                i,j,val: longint;
          Begin
                q1;
                q2;
                i:= 1;
                j:= n;
                val:= 2000000000;

                While (i <= n) and (j >= 1) do
                        Begin
                                If val > abs(b[i] + c[j]) then val:= abs(b[i] + c[j]);
                                If (b[i] + c[j]) >= 0 then dec(j) else inc(i);
                        End;

                Assign(f, output);
                        Rewrite(f);
                        Writeln(f, val);
                Close(f);
          End;

Begin
          init;
          solve;
End.

Code mẫu của skyvn97

import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
    public static void main(String args[]) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        NKSGAME solver = new NKSGAME();
        solver.solve(in, out);
        out.close();
    }
};
class NKSGAME {
    public void solve(InputReader in,PrintWriter out) {
        int n=in.nextInt();
        int[] a=new int[n];
        int[] b=new int[n];
        for (int i=0;i<n;i=i+1) a[i]=in.nextInt();
        for (int i=0;i<n;i=i+1) b[i]=-in.nextInt();
        Arrays.sort(a);
        Arrays.sort(b);
        int j=0;
        int res=INF;
        for (int i=0;i<n;i=i+1) {
            while (j<n && b[j]<a[i]) j++;
            if (j>=n) res=Math.min(res,Abs(a[i]-b[n-1]));
            else {
                res=Math.min(res,Abs(a[i]-b[j]));
                if (j>0) res=Math.min(res,Abs(a[i]-b[j-1]));
            }
        }
        out.println(res);
    }
    private static final int INF=(int)2e9+7;
    private int Abs(int x) {
        return (x<0?-x:x);
    }
};
class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;
    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }
    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
    public int nextInt() {
        return Integer.parseInt(next());
    }
}

Code mẫu của khuc_tuan

#include <cstdio>
#include <algorithm>
using namespace std;

int n, b[100010], c[100010];

int main() {
    scanf("%d",&n);
    for(int i=0;i<n;++i) scanf("%d",b+i);
    for(int i=0;i<n;++i) scanf("%d",c+i);
    sort(b,b+n);
    sort(c,c+n);
    int t=n-1, x, result = 2100000000;
    for(int i=0;i<n;++i)
        for(result<?=abs(b[t]+c[i]);t>0&&abs(b[t-1]+c[i])<=abs(b[t]+c[i]);--t) result <?= abs(b[t-1]+c[i]);
    printf("%d\n",result);
    return 0;
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.