Блог пользователя KP56

Автор KP56, история, 16 месяцев назад, По-английски

Today I've started solving problems on the AtCoder website. For the past 30 minutes I've been trying to solve the "Change Usernames" problem (https://atcoder.jp/contests/abc285/tasks/abc285_d). It seems to be quite simple, but for some reason my solution gets a wrong answer verdict. After trying different approaches (firstly with unordered sets then with cycle detection), I checked the editorial only to notice that the suggested solution is actually the cycle detection one. One thing I noticed is that each of my approaches failed on the same test cases. It seems like I keep doing the same mistake. Can anyone see an issue?

Here is the code:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

#define pb push_back
#define pf push_front
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define endl "\n"
#define in(x) cin >> x
#define inll(x) ll x; in(x)
#define ini(x) int x; in(x)
#define instr(x) string x; in(x)

//https://codeforces.com/blog/entry/62393
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

bool isThereACycle(int at, vector<int> &adj_list, vector<bool> &vis) {
    vis[at] = true;

    if (adj_list[at] == -1) {
        return true;
    } else {
        if (vis[adj_list[at]]) {
            return false;
        }

        return isThereACycle(adj_list[at], adj_list, vis);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ini(n);
    vector<pair<string,string>> strings;
    for (int i = 0; i < n; i++) {
        instr(a);
        instr(b);
        strings.pb(mp(a,b));
    }

    vector<int> adj_list(strings.size(), -1);
    unordered_map<string,int> keys;
    for (int i = 0; i < strings.size(); i++) {
        auto j = strings[i];

        keys[j.first] = i;
    }

    for (int i = 0; i < strings.size(); i++) {
        auto j = strings[i];
        if (keys.find(j.second) != keys.end()) {
            adj_list[i] = keys[j.second];
        }
    }

    vector<bool> vis(strings.size());
    for (int i = 0; i < n; i++) {
        if (!vis[i]) {
            if (isThereACycle(i, adj_list, vis)) {
                cout << "Yes" << endl;
                return 0;
            }
        }
    }

    cout << "No" << endl;
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • -21
  • Проголосовать: не нравится

Автор KP56, история, 21 месяц назад, По-английски

I'm trying to get a custom hash function working for my structure but I get a lot of warnings and compilation fails.

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using lli = long long int;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define endl "\n"
#define in(x) cin >> x
#define in_lli(x) lli (x); in(x)
#define in_i(x) int (x); in(x)
#define in_str(x) string (x); in(x)

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

int current_id = 0;

struct Rectangle {
    int id;

    lli width;
    lli height;
    lli area;

    Rectangle(lli width, lli height) {
        this->width = width;
        this->height = height;
        this->area = width * height;

        id = current_id;
        current_id++;
    }

    inline bool operator == (const Rectangle& rectangle) {
        return rectangle.id == id;
    }

    struct HashFunction {
        size_t operator()(const Rectangle& rectangle) const {
            return hash<int>()(rectangle.id);
        }
    };
};

void readCaseData() {
    //in_lli(rectangles);
    lli rectangles = 100000;
    //in_lli(queries);

    vector<unordered_set<Rectangle,Rectangle::HashFunction>> rect_higher_than(1000);
    vector<unordered_set<Rectangle,Rectangle::HashFunction>> rect_wider_than(1000);

    vector<unordered_set<Rectangle,Rectangle::HashFunction>> rect_shorter_than(1000);
    vector<unordered_set<Rectangle,Rectangle::HashFunction>> rect_narrower_than(1000);
    for (int i = 0; i < rectangles; i++) {
        //in_lli(height);
        //in_lli(width);
        lli height = 0;
        lli width = 0;

        for (int i = width; i < 1000; i++) {
            rect_narrower_than[i].emplace(width, height);
        }

        for (int i = height; i < 1000; i++) {
            rect_shorter_than[i].emplace(width, height);
        }

        for (int i = 999; i >= width; i--) {
            rect_wider_than[i].emplace(width, height);
        }

        for (int i = 999; i >= height; i--) {
            rect_higher_than[i].emplace(width, height);
        }
    }
    
    
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    in_i(cases);

    while (cases--) {
        readCaseData();
    }
}

Does anyone know what the issue may be?

Полный текст и комментарии »

  • Проголосовать: нравится
  • -1
  • Проголосовать: не нравится

Автор KP56, история, 21 месяц назад, По-английски

In the previous contest I submitted a solution which got accepted but right now it says "Time limit exceeded". When I try to resubmit it, it keeps failing test 5. Any ideas why it's not efficient enough?

https://codeforces.com/contest/1722/submission/170472282

Полный текст и комментарии »

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

Автор KP56, история, 23 месяца назад, По-английски

I don't have a lot of experience with c++, but decided to give it a try since I heard it's very good for competitions. My code when used with an older compiler gives a wrong answer and with the new one the solution is correct, but it fails due to a runtime error in test 2. I couldn't find any issue in my code. Am I missing something?

https://codeforces.com/submissions/KP56

Полный текст и комментарии »

  • Проголосовать: нравится
  • -19
  • Проголосовать: не нравится