Cod sursa(job #3222065)

Utilizator adrian_zahariaZaharia Adrian adrian_zaharia Data 8 aprilie 2024 22:58:26
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.95 kb
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define eb emaplce_back
#define ll long long


void solve(){
    int n,k;
    cin>>n>>k;
    deque<int> D;
    int ans = 0;
    bool from_front = true;
    for(int i=1;i<=n;i++){
        int num; cin>>num; D.push_back(num);
    }
    while(!D.empty()){
//        cout<<"k="<<k<<" ,from="<<from_front<<",front="<<D.front()<<", back="<<D.back()<<'\n';
        if(D.size()==1){
            if(D.front()<=k) ans++;
            break;
        }
        if(from_front){
            if(D.front()<=D.back()){
                int suma = 2 * D.front() - 1;
                if(suma<=k){
                    ans++;
                    k-=suma;
                    D.back() -= (D.front()-1);
                    D.pop_front();
                    from_front = false;
                }else break;
            }else{
                int suma = 2 * D.back();
                if(suma<=k){
                    ans++;
                    k-=suma;
                    D.front() -= (D.back());
                    D.pop_back();
                    from_front = true;
                }else break;
            }
        }else{
            if(D.back()<=D.front()){
                int suma = 2 * D.back() - 1;
                if(suma<=k){
                    ans++;
                    k-=suma;
                    D.front() -=(D.back()- 1);
                    D.pop_back();
                    from_front = true;
                }else break;
            }else{
                int suma = 2 * D.front();
                if(suma<=k){
                    ans++;
                    k-=suma;
                    D.back() -=(D.front());
                    D.pop_front();
                    from_front = false;
                }else break;
            }
        }
    }
    cout<<ans<<'\n';


}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin>>t;
    while(t--) solve();
    return 0;
}