首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【第70题】提升递推功力:Codeforces 1848 - B. Vika and the Bridge,和跳石头蛮像的一道题

【第70题】提升递推功力:Codeforces 1848 - B. Vika and the Bridge,和跳石头蛮像的一道题

作者头像
小码匠
发布2023-08-31 15:16:29
发布2023-08-31 15:16:29
3120
举报

【第70题】提升递推公里:Codeforces 1848 - B. Vika and the Bridge,和跳石头蛮像的一道题

题目

题目原文请移步下面的链接

  • https://codeforces.com/problemset/problem/1848/B
  • https://www.luogu.com.cn/problem/CF1848B
    • 参考题解:https://www.luogu.com.cn/problem/solution/CF1848B
  • 标签:OI二分递推

正解

  • 如果有做过跳石头的童鞋会发现两个题蛮像的
  • 同颜色的板子之间记录一下距离,公式
\Rightarrow

当前-上一个-1,因为最优的肯定是在距离最大的两块板子中间涂,所以我们只用记录距离最大的前两位,因为最大的折半后可能会小于第二大的距离

  • 还有就是要算一下两个岸边距离板子的距离,要不会死
  • 中间的数组索引一度把本人搞崩溃
代码
代码语言:javascript
复制
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
 
using namespace std;
#define endl '\n';
 
void best_coder() {
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        int n, k;
        cin >> n >> k;
        vector<int> last(k + 1, -1);
        vector<vector<int>> max_num(k + 1, vector<int>(2, -1));
        vector<int> v(n);
        for (int j = 0; j < n; ++j) {
            cin >> v[j];
            if (j - last[v[j]] - 1 > max_num[v[j]][0]) {
                max_num[v[j]][1] = max_num[v[j]][0];
                max_num[v[j]][0] = j - last[v[j]] - 1;
            } else if (j - last[v[j]] - 1 > max_num[v[j]][1]) {
                max_num[v[j]][1] = j - last[v[j]] - 1;
            }
            last[v[j]] = j;
        }
        for (int j = 1; j <= k; ++j) {
            if (n - last[j] - 1 > max_num[j][0]) {
                max_num[j][1] = max_num[j][0];
                max_num[j][0] = n - last[j] - 1;
            } else if (n - last[j] - 1 > max_num[j][1]) {
                max_num[j][1] = n - last[j] - 1;
            }
        }
        int ans = 0x3f3f3f3f;
        for (int j = 1; j <= k; ++j) {
            ans = min(ans, max(max_num[j][0] / 2, max_num[j][1]));
        }
        cout << ans << endl;
    }
}
 
void happy_coder() {
}
 
int main() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
 
    // 小码匠
    best_coder();
 
    // 最优解
    // happy_coder();
 
    // 返回
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-07-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小码匠和老码农 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 【第70题】提升递推公里:Codeforces 1848 - B. Vika and the Bridge,和跳石头蛮像的一道题
    • 题目
    • 正解
      • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档