Hướng dẫn giải của Tam giác vàng


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

int main()
{
    int n, x[1010], y[1010], ans = 0;

    cin >> n;
    for (int i = 0; i < n; i++) cin >> x[i] >> y[i];

    for (int i = 0; i < n; i++)
    {
        map <long long,int> d;
        for (int j = 0; j < n; j++)
            ans += d[1LL* (x[i] - x[j]) * (x[i] - x[j]) + 1LL * (y[i] - y[j]) * (y[i] - y[j])]++;
    }

    cout << ans << endl;
}

Code mẫu của happyboy99x

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

const int N = 1000 + 2;
pair<long long, int> d[N][N];
long long sd[N][N]; 
int x[N], y[N], n;

long long sqr(long long x) {
    return x * x;
}

long long dis(int u, int v) {
    return sqr(x[u] - x[v]) + sqr(y[u] - y[v]);
}

int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) scanf("%d%d",x+i,y+i);
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) d[i][j] = make_pair(dis(i, j), j), sd[i][j] = d[i][j].first;
        sort(d[i], d[i]+n);
    }
    int res = 0;
    for(int i = 0; i < n; ++i)
        for(int b = 0, e = 1; e <= n; ++e) if(d[i][e].first != d[i][b].first) {
            for(int u = b; u < e; ++u) for(int v = u+1; v < e; ++v)
                res += sd[d[i][u].second][d[i][v].second] != d[i][u].first;
            b = e;
        }
    printf("%d\n", res);
    return 0;
}

Code mẫu của ladpro98

program spoj_triland;
uses    math;
const   fi='';
var     x,y:array[1..1111] of int64;
        f:array[1..1111] of int64;
        n,i,j,c,t:longint;
        inp:text;
        res:int64;
procedure swap(i,j:longint);
var     t:int64;
begin
        t:=f[i];
        f[i]:=f[j];
        f[j]:=t;
end;

procedure sort(l,r:longint);
var     pivot:int64;
        i,j:longint;
begin
        if l>=r then exit;
        pivot:=f[random(r-l+1)+l];
        i:=l;j:=r;
        repeat
                while f[i]<pivot do inc(i);
                while f[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 input;
var     i:longint;
begin
        assign(inp,fi);
        reset(inp);
        readln(inp,n);
        for i:=1 to n do
        begin
                readln(inp,x[i],y[i]);
        end;
        close(inp);
end;

begin
        res:=0;
        input;
        for i:=1 to n do
        begin
                t:=1;

                for j:=1 to i-1 do
                begin
                        f[t]:=sqr(x[i]-x[j])+sqr(y[i]-y[j]);
                        inc(t);
                end;
                for j:=i+1 to n do
                begin
                        f[t]:=sqr(x[i]-x[j])+sqr(y[i]-y[j]);
                        inc(t);
                end;
                t:=n-1;
                sort(1,t);
                j:=1;
                while j<=t do
                begin
                        c:=1;

                        while (f[j]=f[j+1]) do
                        begin
                                inc(c);
                                inc(j);
                        end;
                        inc(res,(c*(c-1)) div 2);
                        inc(j);
                end;

        end;
        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);

struct Point {
    ll x, y;
    Point(ll x = 0, ll y = 0) : x(x), y(y) {}

    Point operator + (Point a) { return Point(x+a.x, y+a.y); }
    Point operator - (Point a) { return Point(x-a.x, y-a.y); }

    void read() {
        int _x, _y; scanf("%d%d", &_x, &_y);
        x = _x; y = _y;
    }

    ll sqlen() { return x*x + y*y; }
} a[1011];

ll x[1011];

int main() {
    int n; scanf("%d", &n);
    FOR(i,1,n) a[i].read();

    long long res = 0;

    FOR(i,1,n) {
        FOR(j,1,n) x[j] = (a[i] - a[j]).sqlen();

        sort(x+1, x+n+1);

        int j = 2;
        FOR(i,2,n) {
            while (j < n && x[j+1] == x[i]) ++j;
            res += j - i;
        }
    }
    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>
#include <cassert>

using namespace std;

typedef long long ll;
typedef 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[] = {0, +1, 0, -1};
const int dc[] = {+1, 0, -1, 0};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;

#define maxn 1005

int n;
ll x[maxn], y[maxn], f[maxn][maxn];



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

    scanf("%d", &n);
    For(i, 1, n) cin >> x[i] >> y[i];
    For(i, 1, n) For(j, 1, n) f[i][j] = sqr(x[i] - x[j]) + sqr(y[i] - y[j]);

    int res = 0;
    For(i, 1, n) {
        vector<ll> V;
        For(j, 1, n) V.pb(f[i][j]);
        V.pb(-1); V.pb(linf);
        sort(all(V));

        int run = 1;
        Rep(i, sz(V) - 1){
            if(V[i] == V[i + 1]) run++;
            else run = 1;
            res += run - 1;
        }
    }

    cout << res;

    return 0;
}

Code mẫu của skyvn97

#include<stdio.h>
#define MAX   2000
typedef unsigned long long ull;
struct point
{
       ull x;
       ull y;
};
struct distance
{
       ull dis;
       ull n;
};
point a[MAX];
ull dist[MAX][MAX];
distance dsort[MAX][MAX];
ull n,res;
void merge(distance a[],int left,int mid,int right)
{
     distance b[MAX];
     distance c[MAX];
     int i,j,k;
     for (i=left;i<=mid;i=i+1) b[i-left+1]=a[i];
     for (i=mid+1;i<=right;i=i+1) c[i-mid]=a[i];
     j=1;
     k=1;
     for (i=left;i<=right;i=i+1)
         {
          if (j>mid-left+1)
             {
              a[i]=c[k];
              k=k+1;
              continue;
             }
          if (k>right-mid)
             {
              a[i]=b[j];
              j=j+1;
              continue;
             }
          if (b[j].dis<c[k].dis)
             {
              a[i]=b[j];
              j=j+1;
              continue;
             }
          a[i]=c[k];
          k=k+1;
         }
}
void mergesort(distance a[],int left,int right)
{
     if (left>=right) return;
     int mid=(left+right)/2;
     mergesort(a,left,mid);
     mergesort(a,mid+1,right);
     merge(a,left,mid,right);
}
void init(void)
{
     ull i,j,k;
     scanf("%llu",&n);
     for (i=1;i<=n;i=i+1)
         {
          scanf("%llu",&a[i].x);          
          scanf("%llu",&a[i].y);
         }
     for (i=1;i<=n;i=i+1)
         for (j=1;j<=n;j=j+1)
             {
              dist[i][j]=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
              dsort[i][j].dis=dist[i][j];
              dsort[i][j].n=j;              
             }
     ull res=0;
     for (i=1;i<=n;i=i+1)
         {
          mergesort(dsort[i],1,n);
          for (j=1;j<n;j=j+1)
              for (k=j+1;k<=n;k=k+1)
                  {
                   if (dsort[i][j].dis<dsort[i][k].dis) break;
                   if (dist[dsort[i][j].n][dsort[i][k].n]!=dsort[i][j].dis) res++;
                  }
         }
     printf("%llu",res);
}
int main(void)
{
    init();
    return 0;
}

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.