社区讨论
昨天ABC的F题代码实现求调
学术版参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lo8ew53j
- 此快照首次捕获于
- 2023/10/27 17:29 2 年前
- 此快照最后确认于
- 2023/10/27 17:29 2 年前
赛时我写的代码也是分别用两种策略,比较大小。
比如在只使用删除操作的策略时,我找到第一个元素后从遍历到,如果当前数字能添加到当前序列的后面就贪心地添加(方法参见代码),然后把剩下的元素加进答案,然后有剩余的操作就弹出最后的元素,但是这样会
CPPWA,请问这种写法的问题在哪里呢? int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i], p[a[i]] = i;
int st;
for(int i = 1; i <= n; i++)
if (p[i] - 1 <= m || (n - p[i] + 1) <= m){
st = i;
break;
}
vector<int> ans, bk, v, t;
if (p[st] - 1 <= m){
// 全用删
ans.push_back(st);
int last = p[st];
int tot = m - p[st] + 1;
for(int i = 1; i <= n; i++){
if (p[i] > last && p[i] - last - 1 <= tot){
tot -= p[i] - last - 1;
ans.push_back(i);
last = p[i];
}
}
for(int i = last + 1; i <= n; i++) ans.push_back(a[i]);
while(ans.size() && tot){
tot--;
ans.pop_back();
}
}
if ((n - p[st] + 1) <= m){
// 把1移到前面
int tot = m - (n - p[st] + 1);
int last = 0;
for(int i = 1; i <= n; i++)
if (p[i] > last && p[i] < p[st] && p[i] - last - 1 <= tot){
tot -= p[i] - last - 1;
v.push_back(i);
last = p[i];
}
for(int i = last + 1; i < p[st]; i++) v.push_back(a[i]);
while(v.size() && tot){
tot--;
v.pop_back();
}
last = v.empty() ? 0 : *v.begin();
for(int i = n; i > p[st]; i--)
if (a[i] < last){
t.push_back(a[i]);
last = a[i];
}
reverse(t.begin(), t.end());
bk.push_back(st);
for(auto x : t) bk.push_back(x);
for(auto x : v) bk.push_back(x);
while(bk.size() && tot){
tot--;
bk.pop_back();
}
if (ans.empty() || bk < ans) ans = bk;
}
for(auto x : ans) cout << x << ' ';
cout << '\n';
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...