808C - Tea Party - CodeForces Solution


constructive algorithms greedy sortings *1400

Please click on ads to support us..

C++ Code:

//SHREE GANESHAYA NAMAH
//OM NAMAHA SHIVAY
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pb push_back
#define pob pop_back
#define mp make_pair
#define ff first
#define ss second
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(__null);
using namespace std;
// const int m=1e9+7;
// const int N=998244353;
// etf in sqrt(n) using prime factorisation
// ll int phi(ll int n){
//     ll int res=n;
//     for(ll int i=2;i*i<=n;i++){
//         if(n%i==0){
//             res/=i;
//             res*=(i-1);
//         }
//         while(n%i==0){
//             n/=i;
//         }
//     }
//     if(n>1){
//         res/=n;
//         res*=(n-1);
//     }
//     return res;
// }
// etf in nlog(logn) using sieve algo
// ll int p[1000001];
// void phif(ll int n){
//     for(ll int i=1;i<=n;i++){
//         p[i]=i;
//     }
//     for(ll int i=2;i<=n;i++){
//         if(p[i]==i){
//             for(ll int j=i;j<=n;j+=i){
//                 p[j]/=i;
//                 p[j]*=(i-1);
//             }
//         }
//     }
//     // for(ll int i=0;i<n;i++){
//     //     cout<<p[i]<<" ";
//     // }
// }
// vector<ll int>factorial(N+20);
// ll int bin(ll int a,ll int b,ll int m){
//     ll int ans=1;
//     while(b>0){
//         if(b&1){
//             ans=(ans*a)%m;
//         }
//         a=(a*a)%m;
//         b>>=1;
//     }
//     return ans%m;
// }
ll int cei(ll int a,ll int b){
    if(a%b==0){
        return a/b;
    }
    else{
        return (a/b)+1;
    }
}
// bool prime(ll int n){
//     for(ll int i=2;i*i<=n;i++){
//         if(n%i==0){
//             return false;
//         }
//     }
//     return true;
// }

 
int main(){
    fastio()
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ll int t=1;
    //cin>>t;
    while(t--){
        ll int n,w;
        cin>>n>>w;
        vector<pair<ll int,ll int>>v,ans;
        ll int s1=0,s2=0;
        for(ll int i=0;i<n;i++){
            ll int x;
            cin>>x;
            s1+=cei(x,2);
            s2+=x;
            v.pb({x,i});
        }
        
        if(w<s1 || w>s2){
            cout<<"-1";
        }
        else{
            ll int fl=0;
            sort(v.rbegin(),v.rend());
            for(ll int i=0;i<n;i++){
                ans.pb({cei(v[i].ff,2),v[i].ss});
                v[i].ff-=cei(v[i].ff,2);
            }
            w-=s1;
            ll int val=0;
            for(ll int i=0;i<n;i++){
                if(w==0){
                    fl=1;
                    break;
                }
                if(v[i].ff>=w){
                    val=w;
                    w=0;
                }
                else{
                    val=v[i].ff;
                    w-=v[i].ff;
                }
                ans[i].ff+=val;
            }
            vector<ll int>res(n);
            for(ll int i=0;i<n;i++){
                res[ans[i].ss]=ans[i].ff;
            }
            for(ll int i=0;i<n;i++){
                cout<<res[i]<<" ";
            }
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle