CSP-J 模拟赛 1
jiangjiangQwQ · · 个人记录
T1
Problem Statement
开始有
Solution
模拟即可
赛时代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 5e5 + 5;
int n, k, a[N];
struct node {
int id, w;
} b[N];
vector<int> person[20];
bool cmp(node x, node y) {
if (x.w == y.w)
return x.id < y.id;
return x.w > y.w;
}
int main() {
freopen("sort.in", "r", stdin);
freopen("sort.out", "w", stdout);
cin >> n >> k;
for (int i = 1; i <= 10; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i].w;
b[i].id = i;
}
sort(b + 1, b + n + 1, cmp);
for (int i = 1; i <= n; i++) {
int x = (i + 1) % 10 + 1;
person[x].push_back(i);
}
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < person[i].size(); j++) {
int x = person[i][j];
b[i].w += a[i];
}
}
sort(b + 1, b + n + 1, cmp);
for (int i = 1; i <= k; i++) cout << b[i].id << ' ';
return 0;
}
赛后爆零了
死因:
int x = person[i][j];
b[i].w += a[i];
改
int x = person[i][j];
b[x].w += a[i];
痛失AK
T2
Problem Statement
给定
Solution
扩展欧拉定理,具体看我博客。
赛时代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
const int N = 1e6 + 5;
ll m, h, a[N], b[N], p;
ll ksm(ll x, ll y) {
ll res = 1;
while (y) {
if (y & 1ll)
res = res * x % m;
x = x * x % m;
y >>= 1ll;
}
return res;
}
ll get(ll x) {
ll phi = x;
for (ll i = 2; i * i <= x; i++) {
if (x % i == 0) {
phi = phi / i * (i - 1);
while (x % i == 0) x /= i;
}
}
if (x != 1)
phi = phi / x * (x - 1);
return phi;
}
ll calc(ll a, ll b) {
if (b < p)
return ksm(a, b);
else
return ksm(a, b % p + p);
}
int main() {
freopen("modulo.in", "r", stdin);
freopen("modulo.out", "w", stdout);
cin >> m >> h;
p = get(m);
ll ans = 0;
for (int i = 1; i <= h; i++) {
cin >> a[i] >> b[i];
ans = (ans + calc(a[i], b[i])) % m;
}
cout << ans;
return 0;
}
T3
Problem Statement
题目描述
小明要去一个国家旅游。这个国家有
所以他就需要选择最先到达的城市,并制定一条路线以城市
现在,你只知道每一条道路所连接的两个城市的相对位置关系,但并不知道所有城市具体的位置。现在对于所有的
Solution
这不拓扑排序模板吗?直接按照题意建出单向边,把度为零的点扔到队列里跑一遍 toposort,然后更新每个点即可。
赛时代码
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
const int N = 3e5 + 5, M = N * 2;
ll n, m;
ll h[N], ne[M], e[M], idx, ru[N];
void add(ll a, ll b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
ru[b]++;
}
ll f[N];
int main() {
freopen("plan.in", "r", stdin);
freopen("plan.out", "w", stdout);
memset(h, -1, sizeof(h));
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
add(u, v);
}
queue<ll> q;
for (int i = 1; i <= n; i++)
if (!ru[i])
q.push(i), f[i] = 1ll;
while (q.size()) {
int u = q.front();
q.pop();
for (int j = h[u]; ~j; j = ne[j]) {
int v = e[j];
f[v] = max(f[v], f[u] + 1ll);
--ru[v];
if (!ru[v])
q.push(v);
}
}
for (int i = 1; i <= n; i++) cout << f[i] << '\n';
return 0;
}
T4
原题
观察到点数很小,根据查询把当前可以重修的城市进行一遍两两松弛,注意
赛时代码
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
const int N = 2e2 + 5, M = 2e5 + 5, inf = 1e9;
int n, m, t[N], q;
int h[N], ne[M], e[M], w[M], idx;
int dist[N][N];
void work(int k) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int main() {
freopen("rebuild.in","r",stdin);
freopen("rebuild.out","w",stdout);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j)
continue;
else
dist[i][j] = inf;
}
}
for (int i = 1; i <= n; i++) cin >> t[i];
for (int i = 1; i <= m; i++) {
int x, y, t;
cin >> x >> y >> t;
x++, y++;
dist[x][y] = dist[y][x] = t;
}
cin >> q;
int expand = 0;
while (q--) {
int x, y, T;
cin >> x >> y >> T;
x++, y++;
if (t[x] > T || t[y] > T) {
cout << -1 << "\n";
continue;
}
while (t[expand + 1] <= T && expand < n) {
expand++;
work(expand);
}
if (dist[x][y] == inf)
cout << -1 << "\n";
else
cout << dist[x][y] << "\n";
}
return 0;
}