thảo luận [Học Tập] Topic thuật toán

1605405732824.png

như tựa đề đấy ạ :((
jGkSyWA.gif
 
ok. Để anh làm cho chú em phần giải thuật. Chú em hãy implement nốt mấy hàm xuất nhập file nhé.

C++:
#include <iostream>
#include <fstream>
// implement these function
// ham doc gia tri N va X
void readNandX(int &N, int &X);
// ham dien cac gia tri vao mang A
void readAarray(int *A, int N);
// ham xuat i, j, k, t
void writeijkt(int i, int j, int k, int t);
// ham xuat impossible
void writeimposible(void);
void Sump4(void)
{
    int N, X;
    readNandX(N, X);
    int *A;
    A = new int[N];
    readAarray(A, N);
    // sau khi co du input dau vao, tien hanh tim 4 index
    for (int i = 0; i < (N - 3); i++)
    {
        for (int j = (i + 1); j < (N - 2); j++)
        {
            for (int k = (j + 1); k < (N - 1); k++)
            {
                for (int t = (k + 1); t < N; t++)
                {
                    if ((A + A[j] + A[k] + A[t]) == X)
                    {
                        // thoa man, ghi 4 index ra file roi out
                        writeijkt(i + 1, j + 1, k + 1, t + 1);
                        return;
                    }
                }
            }
        }
    }
    // new khong thoa man dieu kien, ghi impossible
    writeimposible();
}
 
Last edited by a moderator:
ok. Để anh làm cho chú em phần giải thuật. Chú em hãy implement nốt mấy hàm xuất nhập file nhé.

#include <iostream>
#include <fstream>
// implement these function
// ham doc gia tri N va X
void readNandX(int &N, int &X);
// ham dien cac gia tri vao mang A
void readAarray(int *A, int N);
// ham xuat i, j, k, t
void writeijkt(int i, int j, int k, int t);
// ham xuat impossible
void writeimposible(void);
void Sump4(void)
{
int N, X;
readNandX(N, X);
int *A;
A = new int[N];
readAarray(A, N);
// sau khi co du input dau vao, tien hanh tim 4 index
for (int i = 0; i < (N - 3); i++)
{
for (int j = (i + 1); j < (N - 2); j++)
{
for (int k = (j + 1); k < (N - 1); k++)
{
for (int t = (k + 1); t < N; t++)
{
if ((A + A[j] + A[k] + A[t]) == X)
{
// thoa man, ghi 4 index ra file roi out
writeijkt(i + 1, j + 1, k + 1, t + 1);
return;
}
}
}
}
}
// new khong thoa man dieu kien, ghi impossible
writeimposible();
}
trâu ko nổi bác ơi, 10^3 lận
 
vâng, dùng pointer như bác trên thì chạy full test được
Ờ. Có cách nữa là chú push hết truct chưá value và index low, high của các cặp vào một map. Xong loop các giá trị key của map đó rồi tính giá trị pair bằng p=X-key.val. xong search giá trị pair trong map xem nó có thoả mãn i j t k không.
 
C++:
#include <bits/stdc++.h>

using namespace std;
const int Max = 1000;
class MangTong
{
public:
    int l;
    int r;
    int tong;
};

void ReadData(int a[],int &n,int &x)
{
        cin >> n >> x;
        for(int i=0;i<n;i++)
            cin >> a[i];
}
void Sort(MangTong a[],int n)
{
    for(int i=0;i<n-1;i++)
        for(int j=i+1;j<n;j++)
            if(a[i].tong>a[j].tong){
                MangTong t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
}
bool Check(MangTong a,MangTong b)
{
    if(a.l == b.l || a.r == b.r || a.l == b.r || a.r ==b.l)
        return false;
    return true;
}
void Find(int a[],int n,int x)
{
    int Size = n*(n-1)/2;
    MangTong s[Size];
    int k = 0;
    for(int i = 0;i<n-1;i++)
        for(int j = i+1;j<n;j++)
        {
            s[k].tong = a[i] + a[j];
            s[k].l = i;
            s[k].r = j;
            k++;
        }
    Sort(s,Size);
    int i = 0;
    int j = Size -1;
    while(i<j)
    {
        if(s[i].tong + s[j].tong == x && Check(s[i],s[j]))
        {
            cout << s[i].l+1 << " " << s[i].r+1 << " " << s[j].l+1 <<" " << s[j].r+1;
            break;
        }
        else if(s[i].tong + s[j].tong < x)
            i++;
        else j--;
    }
}
int main()
{
    int a[Max];
    int n,x;
    ReadData(a,n,x);
    Find(a,n,x);
    return 0;
}
e đang tập code c++ nên có gì bác thớt thông cảm, cái này in ra 4 số nhưng chưa theo thứ tự, bác chỉnh sửa lại cho hợp lý.
 
Last edited:
C++:
#include <bits/stdc++.h>
using namespace std;

struct elements{
    int value;
    int index;
};

int main()
{
    iostream::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<elements> arr;
    elements temp;
    for (int i = 1; i <= n; i++)
    {
        cin >> temp.value;
        temp.index = i;
        arr.push_back(temp);
    }

    sort(arr.begin(), arr.end(), [](elements a, elements b){
        return a.value > b.value;
    });
   
    for (int i = 0; i < n - 1; ++i)
        for (int j = i + 1; j < n - 1; ++j)
        {
            int l = j + 1, r = n - 1;
            while (l < r)
            {  
                int sum = arr[i].value + arr[j].value + arr[l].value + arr[r].value;
                if (sum == k)
                {
                    vector<int> output;
                    output.push_back(arr[i].index);
                    output.push_back(arr[j].index);
                    output.push_back(arr[l].index);
                    output.push_back(arr[r].index);
                    sort(output.begin(), output.end());
                    for (auto voz : output)
                        cout << voz << " ";
                    return 0;
                }
                else if (sum < k)
                    l++;
                else
                    r--;
            }
        }
    cout << "IMPOSSIBLE" << endl;
    return 0;
}

Cách này O(n^2 * log(n)) :)
À chết, O(n^3) :beat_brick:
 
  • B1: sort: O(nlogn). Sort trước thì tạo mảng mới là tổng của từng cặp sẽ được sort luôn.
  • B1: Tạo arr1 chứa tổng tất cả các cặp: O(n^2), bộ nhớ O(n^2)
  • B2: Tạo arr2 là sao cho arr2 = X - arr1: O(n^2), bộ nhớ O(n^2)
    [*]B3: Duyệt arr1, tìm trong arr2 phần tử arr2[j] = arr1, O(2*n^2logn)
    [*]B4: Từ i,j suy ngược về index của array ban đầu.


Implement hơi vất vả.
 
Back
Top