码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #702 (Div. 3)G(二分,模拟stl)

时间:2021-02-24 13:01:05      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ble   位置   code   names   contest   --   i++   col   define   

https://codeforces.com/contest/1490/problem/G

二分循环几轮,再二分哪个位置刚好够。

  1 #define IO std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  2 #define bug(x) cout<<#x<<" is "<<x<<endl
  3 #include <bits/stdc++.h>
  4 #define iter ::iterator
  5 using namespace  std;
  6 typedef long long ll;
  7 typedef pair<int,ll>P;
  8 #define pb push_back
  9 #define mk make_pair
 10 #define se second
 11 #define fi first
 12 #define rs o*2+1
 13 #define ls o*2
 14 const ll inf=1e18;
 15 const int N=2e5+5;
 16 int T,n,q;
 17 ll a[N],c[N],d[N];
 18 
 19 struct node{
 20     ll x,y;
 21 }b[N];
 22 bool cmp(node p,node q){
 23     return p.x<q.x;
 24 }
 25 int gao(int x){
 26     int l=1,r=n+1;
 27     while(r-l>1){
 28         int m=(l+r)/2;
 29         if(b[m].x>=x)r=m;
 30         else l=m;
 31     }
 32     if(r>1&&b[r-1].x>=x)r--;
 33     return r;
 34 }
 35 
 36 int gao1(int R,ll x){
 37     int l=0,r=R+1;
 38     while(r-l>1){
 39         int m=(l+r)/2;
 40         ll y=x-d[n]*m;
 41         if(y<=b[n].x)r=m;
 42         else l=m;
 43     }
 44     return r;
 45 }
 46 
 47 int main(){
 48     IO;
 49     cin>>T;
 50     while(T--){
 51         cin>>n>>q;
 52         for(int i=1;i<=n;i++){
 53             cin>>a[i],d[i]=d[i-1]+a[i],b[i].x=b[i-1].x+a[i],b[i].y=i;
 54         }
 55         sort(b+1,b+1+n,cmp);
 56         c[n+1]=1e9;
 57         for(int i=n;i>=1;i--){
 58             c[i]=min(c[i+1],b[i].y);
 59         }
 60         while(q--){
 61             ll x;
 62             cin>>x;
 63             int id=gao(x);
 64             if(id<=n){
 65                 cout<<c[id]-1<<" ";
 66                 continue;
 67             }
 68             if(d[n]<=0){
 69                 cout<<-1<<" ";
 70                 continue;
 71             }
 72             ll y=gao1(x/d[n],x);
 73             ll z=x-y*d[n];
 74             id=gao(z);
 75             cout<<y*n+c[id]-1<<" ";
 76         }
 77         cout<<endl;
 78     }
 79 }
 80 /**
 81 10
 82 2 2
 83 1 -1
 84 1 2
 85 
 86 2 2
 87 0 1
 88 1 2
 89 
 90 
 91 10
 92 2 2
 93 1 0
 94 2 1
 95 
 96 2 2
 97 0 1
 98 1 2
 99 
100 2 2
101 2 0
102 1 2
103 
104 
105 */

 

Codeforces Round #702 (Div. 3)G(二分,模拟stl)

标签:ble   位置   code   names   contest   --   i++   col   define   

原文地址:https://www.cnblogs.com/ccsu-kid/p/14436961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!