Hướng dẫn giải của Ổ khóa đặc biệ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.

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>
#include <algorithm>
using namespace std;

int n,a[5][555],b[255255],cntB[255255],B,c[255255],cntC[255255],C,S;
long long ans;

int modify(int b[],int cntB[],int B)
{
    int res=1;
    sort(b,b+B);
    cntB[0]=1;
    for (int i=1;i<B;i++)
    {
        if (b[i]!=b[res-1]) b[res++]=b[i];
        cntB[res-1]++;
    }
    return res;
}

int main()
{
    cin >> n >> S;
    for (int i=0;i<5;i++)
        for (int j=0;j<n;j++)
            cin >> a[i][j];

    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)   
        {
            b[B++]=a[0][i]+a[1][j];
            c[C++]=a[2][i]+a[3][j];
        }

    B=modify(b,cntB,B);
    C=modify(c,cntC,C);

    for (int i=0;i<n;i++)
    {
        int target=S-a[4][i];
        for (int j=0,k=C-1;j<B && k>=0;)
        {
            long long x=b[j]+c[k];
            if (x==target) ans+=1LL*cntB[j++]*cntC[k--];
            else
                if (x>target) k--;
                else j++;
        }
    }

    cout << ans << endl;
}

Code mẫu của happyboy99x

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

const int N = 500+5;
int a[5][N], n, t[N*N], nx, ny;
pair<int, int> x[N*N], y[N*N];
long long k;

void make(const int &r1, const int &r2, pair<int, int> * x, int &c) {
    int n2 = 0;
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            t[n2++] = a[r1][i] + a[r2][j];
    sort(t, t+n2); t[n2] = INT_MAX; c = 0;
    for(int s = 0, i = 0; i <= n2; ++i) if(t[i] != t[s])
        x[c++] = pair<int, int>(t[s], s-i), s = i;
}

int main() {
    scanf("%d%lld", &n, &k); for(int i = 0; i < 5; ++i) for(int j = 0; j < n; ++j) scanf("%d", &a[i][j]);
    make(0, 1, x, nx); make(2, 3, y, ny);
    long long res = 0;
    for(int i = 0; i < n; ++i) {
        int s = k - a[4][i];
        for(int p = 0, q = ny - 1; p < nx && q >= 0; ) {
            if(x[p].first + y[q].first == s) res += (long long) x[p++].second * y[q--].second;
            else if(x[p].first + y[q].first < s) ++p;
            else --q;
        }
    }
    printf("%lld\n", res);
    return 0;
}

Code mẫu của ladpro98

program c11lock;
uses    math;
const   fi='';
        maxn=505;
type    arr=array[0..maxn*maxn] of longint;
var     a:array[1..5,1..maxn] of longint;
        s1,s2:arr;
        n,d:longint;
        kk,res:int64;

procedure input;
var     inp:text;
        i,j:longint;
begin
        assign(inp,fi);reset(inp);
        readln(inp,n,kk);
        for i:=1 to 5 do
        begin
                for j:=1 to n do read(inp,a[i,j]);
                readln(inp);
        end;
        close(inp);
end;

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

procedure process;
var     t:int64;
        i,j,k,l,r:longint;
begin
        for i:=1 to n do
        for j:=1 to n do
        begin
                inc(d);
                s1[d]:=a[1,i]+a[2,j];
                s2[d]:=a[3,i]+a[4,j];
        end;
        sort(s1,1,d);sort(s2,1,d);
        for k:=1 to n do
        begin
                r:=d;l:=d;
                for i:=1 to d do
                begin
                        t:=kk-a[5,k]-s1[i];
                        while (r>0) and (t<s2[r]) do dec(r);
                        while (l>0) and (t<=s2[l]) do dec(l);
                        if (r>0) and (t=s2[r]) then inc(res,r-l);
                end;
        end;
end;

begin
        input;
        process;
        write(res);
end.

Code mẫu của RR

#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair

#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;

//Buffer reading
int INP,AM,REACHEOF;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
    if(!*inp) { \
        if (REACHEOF) return 0;\
        memset(BUF,0,sizeof BUF);\
        int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
        if (inpzzz != BUFSIZE) REACHEOF = true;\
        inp=BUF; \
    } \
    INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
    AM=0;\
    GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
    if (INP=='-') {AM=1;GETCHAR(INP);} \
    j=INP-'0'; GETCHAR(INP); \
    while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
    if (AM) j=-j;\
}
//End of buffer reading

const long double PI = acos((long double) -1.0);

int n, S, a[10][511];
int x[2][511*511], cnt[2][511*511], nx[2];

int main() {
    while (scanf("%d%d", &n, &S) == 2) {
        FOR(i,1,5) FOR(j,1,n) scanf("%d", &a[i][j]);
        long long res = 0;

        REP(turn,2) {
            int u, v;
            if (turn == 0) u = 2, v = 3;
            else u = 4, v = 5;

            nx[turn] = 0;
            FOR(i,1,n) FOR(j,1,n) {
                x[turn][++nx[turn]] = a[u][i] + a[v][j];
            }
            sort(x[turn]+1, x[turn]+nx[turn]+1);

            int j = 0;
            x[turn][0] = x[turn][1] - 1;
            FOR(i,1,nx[turn]) {
                if (x[turn][i] != x[turn][j]) {
                    ++j;
                    x[turn][j] = x[turn][i];
                    cnt[turn][j] = 1;
                }
                else ++cnt[turn][j];
            }
            nx[turn] = j;
        }

        FOR(t,1,n) {
            int need = S - a[1][t];
            int i = 1, j = nx[1];
            while (i <= nx[0] && j >= 1) {
                if (x[0][i] + x[1][j] == need) {
                    res += cnt[0][i] * (long long) cnt[1][j];
                    ++i; --j;
                }
                else if (x[0][i] + x[1][j] < need) {
                    ++i;
                }
                else --j;
            }
        }

        cout << res << endl;
    }
    return 0;
}

Code mẫu của hieult

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) {
    stringstream ss;
    if (p >= 0)
        ss << fixed << setprecision(p);
    ss << a;
    T r;
    ss >> r;
    return r;
}
template<class T> T gcd(T a, T b) {
    T r;
    while (b != 0) {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}
template<class T> T lcm(T a, T b) {
    return a / gcd(a, b) * b;
}
template<class T> T sqr(T x) {
    return x * x;
}
template<class T> T cube(T x) {
    return x * x * x;
}
template<class T> int getbit(T s, int i) {
    return (s >> i) & 1;
}
template<class T> T onbit(T s, int i) {
    return s | (T(1) << i);
}
template<class T> T offbit(T s, int i) {
    return s & (~(T(1) << i));
}
template<class T> int cntbit(T s) {
    return s == 0 ? 0 : cntbit(s >> 1) + (s & 1);
}

const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc() {
    if (rsz <= 0) {
        ptr = 0;
        rsz = (int) fread(bf, 1, bfsz, stdin);
        if (rsz <= 0)
            return EOF;
    }
    --rsz;
    return bf[ptr++];
}
void ga(char &c) {
    c = EOF;
    while (!isalpha(c))
        c = gc();
}
int gs(char s[]) {
    int l = 0;
    char c = gc();
    while (isspace(c))
        c = gc();
    while (c != EOF && !isspace(c)) {
        s[l++] = c;
        c = gc();
    }
    s[l] = '\0';
    return l;
}
template<class T> bool gi(T &v) {
    v = 0;
    char c = gc();
    while (c != EOF && c != '-' && !isdigit(c))
        c = gc();
    if (c == EOF)
        return false;
    bool neg = c == '-';
    if (neg)
        c = gc();
    while (isdigit(c)) {
        v = v * 10 + c - '0';
        c = gc();
    }
    if (neg)
        v = -v;
    return true;
}

typedef pair<int, int> II;
const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = { -1, 0, +1, 0};
const int dc[] = { 0, +1, 0, -1};
const int inf = (int) 1e9 + 5;
const ll linf = (ll) 1e16 + 5;
const int mod = (ll) (1e9 + 7 + eps);
#define ok puts("ok")
#define maxn 505
#define maxe 40005

map<long long, int> M;
int a[10][maxn], n, k;
ll x[maxn * maxn], y[maxn * maxn];

int main(){
//  freopen("in.txt", "r", stdin);

    cin >> n >> k;
    For(i, 1, 5) For(j, 1, n) cin >> a[i][j];
    int run = 0;
    For(i, 1, n) For(j, 1, n) {
        x[run] = a[1][i] + a[2][j];
        y[run++] = a[3][i] + a[4][j];
    }

    sort(x, x + (n * n)); sort(y, y + (n * n));
    x[n * n] = linf; y[n * n] = linf;

    ll T, A, res = 0;
    For(i, 1, n){
        T = k - a[5][i];
        int u = 0, v = 0;
        for(int j = n * n - 1; j >= 0; j--){
            A = T - x[j];
            while(y[u] < A) u++;
            while(y[v] <= A) v++;
            res += v - u;
        }
    }
    cout << res;

    return 0;

}

Code mẫu của skyvn97

#include<stdio.h>
#include<algorithm>
#define MAX   512
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
struct st
{
       ll v;
       ll r;
};
ll n,k;
ll a[10][MAX];
ll t[MAX*MAX];
ll s[MAX*MAX];
st b[MAX*MAX];
st c[MAX*MAX];
ll i,j,p,q,x;
ull cnt;
ll bsearch(ll left,ll right,ll val)
{
    if (left>right) return 0;
    if (left==right)
       {
        if (b[left].v==val) return (b[left].r);
        else return 0;
       }
    ll mid=(left+right)/2;
    if (b[mid].v==val) return (b[mid].r);
    if (b[mid].v>val) return (bsearch(left,mid-1,val));
    if (b[mid].v<val) return (bsearch(mid+1,right,val));
}
int main(void)
{
    scanf("%lld",&n);
    scanf("%lld",&k);
    for (i=1;i<=5;i=i+1)
        for (j=1;j<=n;j=j+1)
            scanf("%lld",&a[i][j]);
    for (i=1;i<=n;i=i+1)
        for (j=1;j<=n;j=j+1)
            {
             s[i*n-n+j]=a[1][i]+a[2][j];
             t[i*n-n+j]=a[3][i]+a[4][j];
            }    
    sort(&t[1],&t[n*n+1]);
    sort(&s[1],&s[n*n+1]);
    p=0;
    b[0].v=(-3)*10e9;
    for (i=1;i<=n*n;i=i+1)
        {
         if (b[p].v!=s[i])            
            {
             p=p+1;
             b[p].v=s[i];
             b[p].r=1;
            }
         else b[p].r++;
        }
    q=0;
    c[0].v=(-3)*10e9;
    for (i=1;i<=n*n;i=i+1)
        {
         if (c[q].v!=t[i])
            {
             q=q+1;
             c[q].v=t[i];
             c[q].r=1;
            }
         else c[q].r++;
        }
    cnt=0;
    for (x=1;x<=n;x=x+1)
        {
         i=1;
         j=q;
         while ((i<=p) && (j>=1))
               {
                if (b[i].v+c[j].v+a[5][x]==k)
                   {
                    cnt+=b[i].r*c[j].r;
                    i=i+1;
                    j=j-1;
                    continue;
                   }
                if (b[i].v+c[j].v+a[5][x]>k)
                   {
                    j=j-1;
                    continue;
                   }
                i=i+1;
               }
        }
    printf("%llu",cnt);
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.