社区讨论
差分约束WA on #10求条玄关
B4091[CSP-X2020 山东] 分糖果参与者 3已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @mlhpg2a4
- 此快照首次捕获于
- 2026/02/11 15:25 上周
- 此快照最后确认于
- 2026/02/11 16:35 上周
CPP
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 5e4 + 10;
int n, a[maxn], ind[maxn], cnt[maxn];
vector<int>e[maxn];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
a[0] = a[n + 1] = -1e18;
for (int i = 1; i <= n; i++) {
if (a[i] < a[i - 1]) e[i].push_back(i - 1), ind[i - 1]++;
if (a[i] < a[i + 1]) e[i].push_back(i + 1), ind[i + 1]++;
}
if (a[1] < a[n]) e[1].push_back(n), ind[n]++;
if (a[n] < a[1]) e[n].push_back(1), ind[1]++;
queue<int>q;
for (int i = 1; i <= n; i++) if (ind[i] == 0) q.push(i), cnt[i] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : e[u]) {
ind[v]--;
if (ind[v] == 0) {
cnt[v] = cnt[u] + 1;
q.push(v);
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++) ans += cnt[i];
cout << ans;
return 0;
}
尝试拿第一篇题解的代码对拍,但是拍了五十来组没拍出来。
给一下我对拍的代码。
对拍
编译完前三个文件后运行最后一个文件。
gen.cpp
CPP#include<bits/stdc++.h>
using namespace std;
int main() {
freopen("data.in", "w", stdout);
random_device rand;
int n = rand() % 100000 + 1;
cout << n << "\n";
for (int i = 1; i <= n; i++) cout << rand() % 101 << " ";
return 0;
}
mistake.cpp
CPP#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 5e4 + 10;
int n, a[maxn], ind[maxn], cnt[maxn];
vector<int>e[maxn];
signed main() {
freopen("data.in", "r", stdin);
freopen("mistake.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
a[0] = a[n + 1] = -1e18;
for (int i = 1; i <= n; i++) {
if (a[i] < a[i - 1]) e[i].push_back(i - 1), ind[i - 1]++;
if (a[i] < a[i + 1]) e[i].push_back(i + 1), ind[i + 1]++;
}
if (a[1] < a[n]) e[1].push_back(n), ind[n]++;
if (a[n] < a[1]) e[n].push_back(1), ind[1]++;
queue<int>q;
for (int i = 1; i <= n; i++) if (ind[i] == 0) q.push(i), cnt[i] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : e[u]) {
ind[v]--;
if (ind[v] == 0) {
cnt[v] = cnt[u] + 1;
q.push(v);
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++) ans += cnt[i];
cout << ans;
return 0;
}
std.cpp
CPP#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int n, a[N], cnt[N], j, ans;
bool flg;
signed main() {
freopen("data.in", "r", stdin);
freopen("std.out", "w", stdout);
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
cnt[i] = 1;
}
while (1) {
flg = 0;
for (int i = 1; i <= n; i++) {
j = ((i == n) ? 1 : (i + 1));
if (a[i] < a[j] && cnt[i] >= cnt[j]) {
cnt[j] = cnt[i] + 1;
flg = 1;
} else if (a[i] > a[j] && cnt[j] >= cnt[i]) {
cnt[i] = cnt[j] + 1;
flg = 1;
}
}
if (!flg) break;
}
for (int i = 1; i <= n; i++) ans += cnt[i];
printf("%lld", ans);
return 0;
}
tmp.cpp
CPP#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main() {
int cas = 0;
while (true) {
cout << "--------------- #" << ++cas << " ---------------------\n";
system("gen.exe");
system("std.exe");
system("mistake.exe");
if (system("fc std.out mistake.out")) {
break;
}
Sleep(200);
}
}
回复
共 4 条回复,欢迎继续交流。
正在加载回复...