codeforces 1141E 模拟+math?
xg
题意
给你n个数,和H,第pos次,sumpos大于H(sum为前缀和)。求最小的pos,pos属于1到+无穷。输出最小的pos,无解输出-1。
思路
对于1-n。如果sum>=H,则直接输出。
对于大于n。如果sum.n小于H,则无解。
否则,则答案一定为 i+k*n(i为【1,n】,k为【2,+无穷】。
则,遍历1-n,求出对于每一个位置最小的k。然后更新ans即可。
k = (H-sum【i】)/sum + ((H-b[i])%sum != 0)
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map> #include <iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> // #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sp ' ' #define endl '\n' #define inf 0x3f3f3f3f; #define FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl #define P pair<int, int> #define fi first #define se second #define pb(x) push_back(x) #define ppb() pop_back() #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define rep(i,a,b) for(int i=a;i<=b;i++) #define repd(i,a,b) for(int i=a;i>=b;i--) #define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define sca2(a,b) scanf("%d %d",&(a),&(b)) #define sca(a) scanf("%d",&(a)); #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c)) #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b)) #define scall(a) scanf("%lld",&(a)); using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;} const double Pi = acos(-1.0); const double epsilon = Pi/180.0; const int maxn = 2e5+10; ll a[maxn]; ll b[maxn]; int main() { //freopen("input.txt", "r", stdin); ll H, n; cin>>H>>n; ll minn = 0; ll sum = 0; rep(i,1,n){ cin>>a[i]; } rep(i,1,n){ sum += a[i]; b[i] = -sum; if(sum < 0 && abs(sum) >= H){ cout<<i<<endl; return 0; } } if(sum >= 0) { cout<<-1<<endl; return 0; } sum = -sum; ll ans = 9e18; for(ll i = 1; i <= n;++i){ ll t = H-b[i]; ll cnt = (H-b[i])/sum; if((H-b[i])%sum != 0) cnt++; ll tmp = cnt*n + i; ans = min(ans,tmp); } cout<<ans<<endl; }