zoukankan      html  css  js  c++  java
  • Codeforces Round #702 (Div. 3)G(二分,模拟stl)

    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 */
  • 相关阅读:
    Xcode 8.2 想使用插件 怎么办? 教你科学的使用插件
    JSAPI_Ticket签名
    Java中HashMap,LinkedHashMap,TreeMap的区别[转]
    微信支付开发,再次签名,APP调用
    微信支付开发,统一下单
    android studio安装插件
    java实现mysql数据库的备份及还原
    java项目中读取src目录下的文件
    eclipse增加浏览器chrome
    cd 命令
  • 原文地址:https://www.cnblogs.com/ccsu-kid/p/14436961.html
Copyright © 2011-2022 走看看