NC54305. Guest Student
描述
输入描述
The first line of the input contains integer t (1 ≤ t ≤ 10 000) — the number of test cases to process. For each test case independently solve the problem and print the answer.
Each test case consists of two lines. The first of them contains integer k (1 ≤ k ≤ 108) — the required number of days to study as a guest student. The second line contains exactly seven integers a1, a2, …, a7 (ai = 0 or ai = 1) where ai = 1 if and only if classes for guest students are held on the i-th day of a week.
输出描述
Print t lines, the i-th line should contain the answer for the i-th test case — the length of the shortest continuous period of days you need to stay to study exactly k days as a guest student.
示例1
输入:
3 2 0 1 0 0 0 0 0 100000000 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0
输出:
8 233333332 1
说明:
In the first test case you must arrive to the capital of Berland on Monday, have classes on this day, spend a week until next Monday and have classes on the next Monday. In total you need to spend 8 days in the capital of Berland.C++14(g++5.4) 解法, 执行用时: 13ms, 内存消耗: 504K, 提交时间: 2020-10-06 14:12:02
#include<bits/stdc++.h> using namespace std; int main(){ int t,a[8]; scanf("%d",&t); while (t--) { int k,x=0,w,ans=2147483640; scanf("%d",&k); for(int i=1;i<=7;i++) { scanf("%d",&a[i]); x+=a[i]; } for(int i=1;i<=7;i++) { w=k%x+x; int sum=1,j=i; while (1) { if (a[j]==1) w--; j=j%7+1; if(w==0) break; sum++; } ans=min(sum,ans); } printf("%d\n",ans+(k/x-1)*7); } }
C++11(clang++ 3.9) 解法, 执行用时: 28ms, 内存消耗: 420K, 提交时间: 2020-10-06 14:13:04
#include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ int k,s=0,a[101]; cin>>k; for(int i=1;i<=7;i++){ cin>>a[i]; a[i+7]=a[i]; s+=a[i]; } int ans=1e9; for(int i=1;i<=7;i++){ int n=k; for(int j=i;j<=i+7;j++){ n-=a[j]; if(n%s==0){ ans=min(ans,j-i+1+n/s*7); } } } cout<<ans<<endl; } return 0; }