Блог пользователя Voldemort_007

Автор Voldemort_007, история, 3 года назад, По-английски

Can somebody explain why the Greedy solution work for this question and why it works only when using armor not health as a rule to make a descision and what will be its time complexity.

Question link — https://www.spoj.com/problems/DIEHARD/

#include<iostream>
#include<bits/stdc++.h>
#define ll long long int
#define F first
#define S second
#define foi(i,s,e) for(ll i = s;i <= e;i++)
#define fod(i,s,e) for(ll i = s;i >= e;i--)
using namespace std;
#define mod 1000000007
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

struct cmp{
    bool operator()(pair<ll,ll>a,pair<ll,ll>b)
    {
        return a.first < b.first;
    }
};

int main()
{
    fast();
    ll t;
    cin >> t;
    foi(z,0,t-1)
    {
        int health, armor;
        cin >> health >> armor;

        int time = 0;
        while(1)
        {
            if(health <= 0 || armor <= 0)
                break;

            if(time % 2 == 0)
            {
                health += 3;
                armor += 2;
            }
            else
            {
                    if(armor > 10)
                    {
                        health -= 5;
                        armor  -= 10;
                    }
                    else
                    {
                        health  -= 20;
                        armor += 5;
                    }
            }
            time++;
        }
        cout << time-1 << "\n";
    }
}


Полный текст и комментарии »

  • Проголосовать: нравится
  • -24
  • Проголосовать: не нравится

Автор Voldemort_007, история, 3 года назад, По-английски

Question Link —

https://practice.geeksforgeeks.org/problems/game-with-string4100/1#

Method 1 —

int minValue(string a, int k){

int freq[26] = {0};

    //O(n)
    for(int i = 0;i < a.size();i++)
        freq[a[i] - 'a']++;

    //O(26)
    priority_queue<int>q(freq,freq+26);


    //O(k * log(26)
    while(k && a.size() > 0)
    {
        int p = q.top();
        q.pop();
        k--;
        q.push(p-1);
    }

    //O(26*log26)
    int ans = 0;
    while(q.size() > 0)
    {
        int p = q.top();
        ans += p * p;
        q.pop();
    }
    return ans;
}

Time — O(k*log26) Space — O(26)

Method 2 —

int minValue(string a, int k){

unordered_map<char,int>mp;

    //O(n)
    for(int i = 0;i < a.size();i++)
        mp[a[i]]++;

    priority_queue<int>q;

    //O(n*logn) in worstCase
    for(auto i : mp)
        q.push(i.second);

    //O(k * log(n)
    while(k && a.size() > 0)
    {
        int p = q.top();
        q.pop();
        k--;
        q.push(p-1);
    }

    //O(n*logn)
    int ans = 0;
    while(q.size() > 0)
    {
        int p = q.top();
        ans += p * p;
        q.pop();
    }
    return ans;
}

Time — O(nlogn) Space — O(n)

Полный текст и комментарии »

  • Проголосовать: нравится
  • -20
  • Проголосовать: не нравится

Автор Voldemort_007, история, 4 года назад, По-английски

Seven line segments, with lengths no greater than 10 inches, and no shorter than 1 inch, are given. Show that one can choose three of them to represent the sides of a triangle.

How to Prove it Using Pigeon Hole Principle.

Полный текст и комментарии »

  • Проголосовать: нравится
  • -6
  • Проголосовать: не нравится

Автор Voldemort_007, история, 4 года назад, По-английски

Can Anybody suggest Some good Source to learn PIGEONHOLE PRINCIPLE

Полный текст и комментарии »

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится