1 条题解

  • 0
    @ 2025-8-24 22:51:55

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar Genshineer
    这个家伙很懒,除了什么什么也没有留下.

    搬运于2025-08-24 22:51:55,当前版本为作者最后更新于2023-10-28 21:20:13,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    考虑将中间经过的时间分成三段:若干个整星期,前面的散块,后面的散块。

    可以先考虑没有前面的散块的做法:

    • 设经过了 resres 个整星期,记每个整星期有 cntcnt 天有空,显然中间每次有空都选择听课是最优的,可以发现 res=7×k1cntres=7\times\lfloor\dfrac{k-1}{cnt}\rfloor,此时剩下需要安排的课程数为 kres7×cntk-\lfloor\dfrac{res}{7}\rfloor\times cnt。由于剩下的天数不超过 77,所以可以直接扫过整个星期并累加答案。
    int get(int x) {
    	int res = 7 * ((x - 1) / cnt);
    	x -= (res / 7) * cnt;
    	for (int i = 1; i <= 7 and x; i++) {
    		res++;
    		x -= a[i];
    	}
    	return res;
    }
    

    接下来可以发现,前面的散块只有 66 种情况,即以一个星期的每天作为开头的散块,不妨考虑直接“旋转” a 数组,从星期一开头开始枚举,每次统计完答案直接把开头的元素扔到数组尾部。可以证明,这一做法与直接处理前面的散块是等价的,从而极大地降低了程序地实现难度。

    #include <bits/stdc++.h>
    #define int long long
    using namespace std;
    
    int TestCase, k, a[10], ans, cnt;
    
    void rtt() { // Rotate
    	int tmp = a[1];
    	for (int i = 1; i < 7; i++) a[i] = a[i + 1];
    	a[7] = tmp;
    }
    
    int get(int x) {
    	int res = 7 * ((x - 1) / cnt);
    	x -= (res / 7) * cnt;
    	for (int i = 1; i <= 7 and x; i++) {
    		res++;
    		x -= a[i];
    	}
    	return res;
    }
    
    signed main() {
    //Fast IO
    	ios::sync_with_stdio(false);
    	cin.tie(nullptr), cout.tie(nullptr);
    
    	cin >> TestCase;
    	while (TestCase--) {
    		cin >> k;
    		cnt = 0, ans = 1e18;
    		for (int i = 1; i <= 7; i++) cin >> a[i], cnt += a[i];
    		for (int i = 1; i <= 7; i++) {
    			ans = min(ans, get(k));
    			rtt();
    		}
    		cout << ans - 1<< "\n";
    	}
    }
    

    另给出官方题解供参考:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define forn(i, n) for (int i = 0; i < int(n); i++)
    
    const int N = 7;
    
    int main() {
        int testCaseCount;
        cin >> testCaseCount;
        forn(testCase, testCaseCount) {
            int k;
            cin >> k;
            vector<int> a(N);
            forn(i, N)
                cin >> a[i];
            int sum = accumulate(a.begin(), a.end(), 0);
            int result = INT_MAX;
            forn(i, N)
                if (a[i] == 1) {
                    int n = max(0, k / sum - 1);
                    int j = i, cur = n * sum, m = n * 7;
                    while (cur < k) {
                        cur += a[j];
                        j = (j + 1) % N;
                        m++;
                    }
                    result = min(result, m);
                }
            cout << result << endl;
        }
    }
    
    • 1

    信息

    ID
    8901
    时间
    2000ms
    内存
    512MiB
    难度
    2
    标签
    递交数
    0
    已通过
    0
    上传者