Изменения рейтингов за последние раунды временно удалены. Скоро они будут возвращены. ×

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

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

Hey folks, how the below code from here has O(√n) time complexity? Shouldn't it be equal to the number of prime factors of n?

vector<long long> trial_division1(long long n) {
    vector<long long> factorization;
    for (long long d = 2; d * d <= n; d++) {
        while (n % d == 0) {
            factorization.push_back(d);
            n /= d;
        }
    }
    if (n > 1)
        factorization.push_back(n);
    return factorization;
}

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

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

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

Hello everyone, I have been trying to find shortest path using dijkstra with set by storing indices instead of index-distance pairs with the help of a custom comparator. It is failing on a very large test case and I am having a difficult time debugging it. Where am I doing wrong? Any type of hint would be appreciated.

The Problem My solution

vector<ll> d;
 
struct comp {
	bool operator()(const int& a, const int& b) const {
		return d[a] < d[b];
	}
};
 
void solve() {
	int n, m; cin >> n >> m;
	vector<vector<pii>> graph(n + 1);
	set<int, comp> q;											
	rep(m) {
		int a, b, c; cin >> a >> b >> c;
		graph[a].push_back(make_pair(b, c));
		graph[b].push_back(make_pair(a, c));
	}
	vector<int> p(n + 1, -1);
	d.resize(n + 1, LLONG_MAX);
	d[1] = 0;
	q.insert(1);
	while(!q.empty()) {
		int node = *q.begin();
		q.erase(q.begin());
		for(auto adj : graph[node]) {
			if(d[adj.ff] > d[node] + adj.ss) {
				if(q.find(adj.ff) != q.end()) q.erase(adj.ff); 
				d[adj.ff] = d[node] + adj.ss;
				p[adj.ff] = node;
				q.insert(adj.ff);
			}	
		}
	}
	if(d[n] == LLONG_MAX) cout << -1 << endl;
	else {
		vector<int> path;
		while(n != -1) {
			path.push_back(n);
			n = p[n];
		}
		rep(path.size()) cout << path[path.size() - i - 1] << ' '; cout << endl;
	}
}

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

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

Автор vaibhavmisra, история, 2 года назад, По-английски

Operation : Change any array element to arbitrary integer, O(N^2) would give TLE, My approach : ans = Length of array — Length of longest non-decreasing sub-sequence because I want maximum length already sorted, TC : O(NlgN),
Is this approach correct? Any other approaches?

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

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