跳到正文

个人记录

猿编程 CSP-J Round 2 题解

jsc_want_AK_IOI约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年6月8日
收录
2026年6月8日
更新
2026年11月14日
篇幅
约 9 分钟

版本

  1. v1当前

经典的鸡兔同笼问题:

设 有 xx 个单人碰碰车,yy 个双人碰碰车。

y=(t−x×1)÷(2−1)y=t−xx=c−yy = (t - x \times 1) \div (2 - 1)\\ y = t - x\\ x = c - y
Code
C++
#include<bits/stdc++.h>

using namespace std;

#define ll long long

signed main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	ll c, t; cin >> c >> t;
	ll y = t - c, x = c - y;
	cout << x << " " << y;
	return 0;
}

这道题解释将整个序列视为从左向右的通道。设 ndnd 表示当前可承载的沙子容量。 从左到右扫描:

  • 每多一个位置,通道容量增加 i−1i-1;
  • 如果 ai≤nda_i\le nd,则全部通过,消耗容量;
  • 否则超出部分无法到达位置 11,那就直接计入答案并清空通道。
Code
C++
#include <bits/stdc++.h>

using namespace std;
 
const int MAXN = 2e5 + 5;
 
int n;
int a[MAXN];
 
int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    long long nd = 0, ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        nd += i - 1; // 新增一条通道容量
        if (a[i] <= nd)
            nd -= a[i]; // 新增一条通道容量
        else
        {
            ans += a[i] - nd; // 新增一条通道容量
            nd = 0; // 新增一条通道容量
        }
    }
    cout << ans << "\n";
    return 0;
}

本题本质是 滑动窗口 + 状态维护。

九莲宝灯的结构是固定的:

  • 需要 99 种连续点数 x∼x+8x \sim x+8
  • 首尾两种至少 33 张
  • 中间七种至少 11 张

我们可以维护 cnt[]cnt[] 表示当前窗口中各点数出现次数,并对每个可能的 xx 进行计算,与就是代码中的 chkchk 函数。滑动窗口时,他最多只会影响 99 个 chkchk 值,这就可以算出正确答案。

Code
C++
#include <bits/stdc++.h>
using namespace std;
 
const int MAXN = 2e5 + 5;
 
int n, m;
int a[MAXN];
int sum[MAXN];
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while(T--)
    {
        cin >> n >> m;
	    for (int i = 1; i <= m; i++)
	        sum[i] = 0;
	    for (int i = 1; i <= n; i++)
	    {
	        cin >> a[i];
	        sum[a[i]]++;
	    }
	    for (int i = 2; i <= m; i++)
	        sum[i] += sum[i - 1];
	    int x = -1;
	    for (int i = m; i >= 1; i--)
	    {
	        int rt = 0, op = 0;
	        for (int j = 1; i * j <= m; j++)
	        {
	            int l = i * j;
	            int r = i * j + j;
	            l = max(l, rt + 1);
	            r = min(r, m);
	            if (l <= r)
	                op += sum[r] - sum[l - 1];
	            rt = max(rt, r);
	        }
	        if (op == n)
	        {
	            x = i;
	            break;
	        }
	    }
	    ll ans = 0;
	    for (int i = 1; i <= n; i++)
	        ans += (a[i] + x) / (x + 1);
	    cout << ans << "\n";
    }
    return 0;
}

有 nn 条隧道,第 ii 条长度为 aia_i。
你需要选一个正整数 xx,每天可以对任意一条隧道挖 xx 或 x+1x+1 的长度,直到恰好挖完。
问最少需要多少天。

  • 如果 xx 是合法的,则 每个 aia_i 都能写成一些 xx 和一些 x+1x+1 的和。
  • 等价于:对任意 aia_i,都存在非负整数 p,qp,q 使得
ai=p⋅x+q⋅(x+1)a_i = p\cdot x + q\cdot(x+1)
  • 观察可知:只要 ai≥xa_i \ge x,就一定能被凑出来。
  • 因此合法条件就变成了:最小的 ai≥xa_i \ge x。

于是我们从大到小枚举 xx,检查是否所有 ai≥xa_i \ge x。
找到最大的合法 xx 后,对每条隧道贪心:
每天尽量用 x+1x+1 挖,需要的天数就是 ⌈aix+1⌉\lceil \frac{a_i}{x+1} \rceil。

Code
C++
#include <bits/stdc++.h>
using namespace std;
 
const int MAXN = 2e5 + 5;
 
int n, m;
int a[MAXN];
int sum[MAXN];
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while(T--)
    {
        cin >> n >> m;
	    for (int i = 1; i <= m; i++)
	        sum[i] = 0;
	    for (int i = 1; i <= n; i++)
	    {
	        cin >> a[i];
	        sum[a[i]]++;
	    }
	    for (int i = 2; i <= m; i++)
	        sum[i] += sum[i - 1];
	    int x = -1;
	    for (int i = m; i >= 1; i--)
	    {
	        int rt = 0, op = 0;
	        for (int j = 1; i * j <= m; j++)
	        {
	            int l = i * j;
	            int r = i * j + j;
	            l = max(l, rt + 1);
	            r = min(r, m);
	            if (l <= r)
	                op += sum[r] - sum[l - 1];
	            rt = max(rt, r);
	        }
	        if (op == n)
	        {
	            x = i;
	            break;
	        }
	    }
	    ll ans = 0;
	    for (int i = 1; i <= n; i++)
	        ans += (a[i] + x) / (x + 1);
	    cout << ans << "\n";
    }
    return 0;
}
原文