CodeRazer's blog

By CodeRazer, history, 20 months ago, In English

Problem: https://dmoj.ca/problem/ioi00p1

I have written the following code for this problem (got the same problem accepted on leetcode with the same code).

#include <iostream>
#include <iomanip>
#include <array>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <cmath>
#include <climits>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
const int MOD = (int)1e9 + 7;

using namespace std;

int dp[5000][5000];

int solve(int left, int right, string& s) {
    if (dp[left][right] != -1) {
        return dp[left][right];
    }
    if (left >= right) {
        return 0;
    }
    if (s[left] != s[right]) {
        dp[left][right] = min(1 + solve(left, right - 1, s), 1 + solve(left + 1, right, s));
    } else {
        dp[left][right] = solve(left + 1, right - 1, s);
    }
    return dp[left][right];
}

int main() {
    int n; cin >> n;
    string s; cin >> s;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            dp[i][j] = -1;
        }
    }
    cout << solve(0, n - 1, s) << '\n';
}

This gives RTE (segmentation fault) for some reason. I don't understand what I'm doing wrong.

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By CodeRazer, history, 2 years ago, In English

Hello, I was wondering how long will it take for the problem ratings of round #821 and #822 to be displayed.

Thank you.

Full text and comments »

  • Vote: I like it
  • -3
  • Vote: I do not like it

By CodeRazer, history, 2 years ago, In English

Problem

279B - Книги

My approach

The total time taken to read all books is sum. Then for every iteration if the sum is greater than the free time you can either remove the first book or the last book in the series, it's better to remove the book with the higher read duration for optimal solution (It is optimal because, since we have to remove a book, it's better to remove the one with the higher read duration to have more potential time for reading other books). If there are any doubts regarding the approach, refer to the code.

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#define ll long long

using namespace std;

int main(void) {
    ll n, t; cin >> n >> t;
    vector<ll> a(n);
    ll sum = 0;
    for (ll i = 0; i < n; ++i) {
        cin >> a[i];
        sum += a[i];
    }
    ll lo = 0;
    ll hi = n - 1;
    while (sum > t && hi >= lo) {
        if (a[lo] > a[hi]) {
            sum -= a[lo];
            lo++;
        } else {
            sum -= a[hi];
            hi--;
        }
    }
    cout << hi - lo + 1 << endl;
}

Issue

Obviously my approach or program (or both) are incorrect, but I'm having a hard time finding what is wrong. I would appreciate any help. Thanks in advance :)

Full text and comments »

  • Vote: I like it
  • +2
  • Vote: I do not like it

By CodeRazer, history, 3 years ago, In English

The following code gives the correct result on my machine, but doesn't give the right answer on codeforces. 125707404 any help will be be appreciated

#include <stdio.h>
#include <stdlib.h>
 
int comparator (const void * p1, const void * p2)
{
  return (*(double*)p1 - *(double*)p2);
}
 
double maxx(double a, double b) {
    if (a > b){
        return a;
    }
    else{
        return b;
    }
}
 
int main() {
    int n;
    double l;
    scanf("%d %lf", &n, &l);
    double a[n+1];
    for (int i = 0; i<n; ++i) {
        scanf("%lf", &a[i]);
    }
    qsort(a, n, sizeof(double), comparator);
    double maxdiff = 0;
    double prev = a[0];
    maxdiff = (a[0] - 0)*2;
    for (int i = 1; i<n; ++i){
        maxdiff = maxx(maxdiff, a[i] - prev);
        prev = a[i];
    }
    maxdiff = maxx(maxdiff, l*2 - prev*2);
    double ans;
    ans = maxdiff/2;
 
    printf("%lf\n", ans);
    return 0;
}

Full text and comments »

  • Vote: I like it
  • +10
  • Vote: I do not like it