이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>
using namespace std;
const int INF = (int)1e9 + 7;
struct T
{
int pos;
int delta;
};
signed main()
{
#ifdef ONPC
FILE* stream;
freopen_s(&stream, "input.txt", "r", stdin);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
int dim, cntofs;
cin >> dim >> cntofs;
vector<T> ofs(cntofs);
vector<vector<int>> cine(dim);
for (int i = 0; i < cntofs; i++)
{
cin >> ofs[i].pos >> ofs[i].delta;
assert(0 <= ofs[i].pos && ofs[i].pos < dim);
cine[ofs[i].pos].push_back(i);
}
vector<int> dp(dim, INF);
priority_queue<pair<int, int>> pq;
auto relax = [&](int i, int val)
{
if (val < dp[i])
{
dp[i] = val;
pq.push({ -dp[i], i });
}
};
relax(ofs[0].pos, 0);
while (!pq.empty())
{
auto it = pq.top();
pq.pop();
int loc = it.second;
if (-it.first != dp[loc])
{
continue;
}
for (auto& i : cine[loc])
{
for (int loc2 = loc % ofs[i].delta; loc2 < dim; loc2 += ofs[i].delta)
{
int posdif = abs(ofs[i].pos - loc2);
relax(loc2, dp[loc] + posdif / ofs[i].delta);
}
}
}
for (int i = 0; i < dim; i++)
{
//cout << i << " ---> " << dp[i] << "\n";
}
int sol = (dp[ofs[1].pos] == INF) ? (-1) : (dp[ofs[1].pos]);
cout << sol << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |