constructive algorithms greedy sortings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <vector>
#define pb push_back
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define ll  long long int 
#define gt greater<ll>()
#define ff first
#define ss second
#define pi acos(-1)
#define ull unsigned long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define  mod 1000000007;
#define bi_one(n)      __builtin_popcount(n)
#define str(a)          a.begin(),a.end()
#define mem(a,b)        memset(a, b, sizeof(a) )
#define max_ele(a,n)    *max_element(a,a+n)
#define min_ele(a,n)    *min_element(a,a+n)
#define fio()                     \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define sp(n) fixed << setprecision(n)
#define Erase(s) s.erase(unique(s.begin(), s.end()), s.end())
#define fn0 for (i = 0; i < n; i++)
#define fn1 for(i=1; i<=n; i++)
#define upper(v,val) upper_bound(v.begin(), v.end(), val)-v.begin()
#define lower(v,val) lower_bound(v.begin(), v.end(), val)-v.begin()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
using namespace std;
const int N=2e5+5;
vector<int> g[N];
bool vis[N];
int n,m,ans=0;
vector<ll>v;
map<ll,ll>mp;
void print(vector<ll> &v)
{
    for (ll i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << endl;
}

void solution()
{
    ll n, m,sq, l = 0,r=0, k = 0, i, j, a=0, b=0, c = 0, d = 0, d1 = 0, sum = 0;
    string s,s1="";
    cin>>n>>m>>k;
    vector<vector<ll>>v;
    fn0
    {
        vector<ll>vv;
        for(j=0; j<m; j++)
        {
            cin>>a;
            vv.pb(a);
        }
        sort(all(vv));
        vector<ll>pp;
        sum=0;
        for(j=0; j<m; j++)
        {
            sum+=vv[j];
            pp.pb(sum);
        }
        v.pb(pp);
    }
    
    vector<pair<ll,ll>> dd;
    for(i=0; i<v.size(); i++)
    {
        c=0;
        sum=0;
        for(j=0; j<v[i].size(); j++)
        {
            if(v[i][j]<=k)
            {
                c++;
                sum+=v[i][j];
            }
            else
                break;

        }
        //cout<<c<< " "<<sum<<endl;
        dd.pb({c,sum});
    }

    d=dd[0].ff;
    d1=dd[0].ss;
    c=1;
    for(i=1; i<dd.size(); i++)
    {
        if(dd[i].ff>d)
            c++;
        else if(dd[i].ff==d)
        {
            if(dd[i].ss<d1)
                c++;
        }
    }
    cout<<c<<endl;
}

int main()
{
    fio();
    int tt = 1;
    cin >> tt;
   // int x=1;
    while (tt--)
    {
        //cout<<"Case "<<x<< ": ";
        solution();
        //x++;
    }
}


Comments

Submit
1 Comments
  • 5/10/2023 21:41 - Africa/Cairo

#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double
#define el "\n"
const ll N = 1e5 + 1, INF = 1e18, m = 1e9 + 7, OO = LONG_MAX;
vector <int>vec;
vector<pair<int, int>>pa;
signed main() {
    int t = 1; cin >> t;
    while (t--) {
        int n, m, h;
        cin >> n >> m >> h;
        ll  all = 0, c = 0;
        int ri=0, le=0;
        for (int i = 0; i < n; i++) {
            for (int ii = 0; ii < m; ii++) {
                int a; cin >> a;
                vec.push_back(a);
            }
            sort(vec.begin(), vec.end());
            all = 0, c = 0;
            for (int k = 1; k < m; k++) {
                vec[k] += vec[k - 1];
            }
            for (auto a : vec) {
                if ( a <= h) {
                    c--;
                    all += a;
                }
                else break;
            }
            if (i == 0) {
                le = c;
                ri = all;
            }
            pa.push_back({ c,all });
            vec.clear();
        }
        sort(pa.begin(), pa.end());
        int ans = 0;
        for (auto a : pa) {
            ans++;
            if (a.first == le && a.second == ri) {
                cout << ans << "\n";
                break;
            }
        }
        pa.clear();
    }
    return 0;
}


More Questions

112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie