Luyện Tập Cơ Bản Thuật Toán Mùa Đông NowCoder 1

Luyện Tập Mùa Đông NowCoder - Phần 1

Dễ

A-Tìm Kiếm DFS

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
#define int long long

void giai()
{
    int n;
    cin >> n;
    
    string s;
    cin >> s;
    
    map<char, bool> mapD, mapDCap;
    bool coD = false, coDCap = false;
    
    for(int i = 0; i < s.size(); i++)
    {
        if(s[i] == 'd') coD = true;
        if(s[i] == 'D') coDCap = true;
        
        if(s[i] == 'f' && coD) mapD[s[i]] = true;
        if(s[i] == 'F' && coDCap) mapDCap[s[i]] = true;
        
        if(s[i] == 's' && mapD['f']) mapD[s[i]] = true;
        if(s[i] == 'S' && mapDCap['F']) mapDCap[s[i]] = true;
        
        if(mapD['s'] && mapDCap['S']) break;
    }
    
    if(mapDCap['S']) cout << 1 << " ";
    else cout << 0 << " ";
    
    if(mapD['s']) cout << 1 << endl;
    else cout << 0 << endl;
}

signed main()
{
    int t;
    cin >> t;

    while(t--)
    {
        giai();
    }

    return 0;
}

Trung bình

C-Phân Phối Theo Hàng

Khi có người chèn hàng, thời gian chờ của mỗi người phía sau sẽ tăng lên tc phút. Tổng độ bất mãn là tc * (số người phía sau). Với độ bất mãn tối đa M, ta tính số người tối đa phía sau là M / tc. Thời gian sớm nhất để người đó hoàn thành là tổng thời gian của những người phía trước cộng thêm t.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e8 + 10;
typedef long long LL;
LL pre[N];

int main()
{
    LL n, q, t;
    cin >> n >> q >> t;
    
    for(int i = 1; i <= n; i++) cin >> pre[i];
    
    sort(pre + 1, pre + 1 + n);
    
    for(int i = 1; i <= n; i++) pre[i] += pre[i - 1];
    
    while(q--)
    {
        LL x;
        cin >> x;
        
        LL pos = x / t;
        
        cout << pre[max((LL)0, n - pos)] + t << endl;
    }

    return 0;
}

Trung bình - Khó

D-Mảng Thành Gà

Để kiểm tra số x có thể tạo thành từ mảng, ta xem xét các phép biến đổi giá trị phần tử sao cho tích không vượt quá 1e9. Với n >= 30, nếu số lượng phần tử lớn hơn 30, tích sẽ vượt ngưỡng. Ngược lại, thử tất cả các giá trị có thể trong khoảng -1e5 đến 1e5.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
#define int long long

void giai()
{
    int n, m;
    cin >> n >> m;
    map<int, int> dem;
    set<int> ketQua;
    ketQua.insert(0);
    vector<int> a(n);
    
    for(int i = 0; i < n; i++)
    {
        cin >> a[i];
        dem[a[i]]++;
    }
    
    if(n >= 30)
    {
        for(auto [x, y] : dem)
        {
            if(n - dem[x] - dem[x - 2] > 30) continue;
            
            int tich = 1, ok = 1;
            for(int i = 0; i < n; i++)
            {
                tich *= (a[i] - (x - 1));
                if(abs(tich) > 1e9)
                {
                    ok = 0;
                    break;
                }
            }
            if(ok) ketQua.insert(tich);
            
            tich = 1, ok = 1;
            for(int i = 0; i < n; i++)
            {
                tich *= (a[i] - (x + 1));
                if(abs(tich) > 1e9)
                {
                    ok = 0;
                    break;
                }
            }
            if(ok) ketQua.insert(tich);
        }
    }
    else
    {
        sort(a.begin(), a.end());
        int minGiaTri = a[0];
        for(int i = 0; i < n; i++) a[i] -= minGiaTri;
        
        for(int i = -1e5; i <= 1e5; i++)
        {
            int tich = 1, ok = 1;
            for(int j = 0; j < n; j++)
            {
                tich *= (a[j] + i);
                if(abs(tich) > 1e9)
                {
                    ok = 0;
                    break;
                }
            }
            if(ok) ketQua.insert(tich);
        }
        
        reverse(a.begin(), a.end());
        int maxGiaTri = a[0];
        for(int i = 0; i < n; i++) a[i] -= maxGiaTri;
        
        for(int i = -1e5; i <= 1e5; i++)
        {
            int tich = 1, ok = 1;
            for(int j = 0; j < n; j++)
            {
                tich *= (a[j] + i);
                if(abs(tich) > 1e9)
                {
                    ok = 0;
                    break;
                }
            }
            if(ok) ketQua.insert(tich);
        }
    }

    while(m--)
    {
        int x;
        cin >> x;
        cout << (ketQua.count(x) ? "Yes" : "No") << endl;
    }
}

signed main()
{
    int t = 1;
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    while(t--) giai();
    return 0;
}

Thẻ: thuật toán DFS Tham lam mạng STL

Đăng vào ngày 23 tháng 6 lúc 01:44