c++ - 需要帮助来修复我的代码以执行特定的逻辑

标签 c++ c++14 multiset

我试图从Codechef论坛寻求帮助,但无法获得任何重大帮助,因此我在这里发布了我的问题!
问题是从7月开始对Codechef进行长期挑战。
问题链接:https://www.codechef.com/JULY20B/problems/DRCHEF
代码链接:https://www.codechef.com/viewsolution/35869168
我的代码__

#include <iostream>
#include <set>
#include <iterator>
#include <stdio.h>

using namespace std ;

// it returns highest value present in multiset within the range [x,2x]
long int range_search (multiset <long int> S, long int X)

{
    multiset<long int>::iterator it1, it2;
    it1 = S.lower_bound(X);
    it2 = S.upper_bound(2 * X);
    long int ans;

    if (it1 != it2)
    {
        --it2;
        ans = *(it2);
    }

    else
    {
        ans = -1;
    }

    return ans;
}



// driver function
int main () {
    int T;
    cin>>T;

    for (int k =0; k<T; k++){
        int N ;
        long int x ;
        scanf("%d",&N) ;
        scanf ("%ld",&x) ;
        multiset<long int> s ;  // storing country populations in multiset
        multiset<long int>::iterator itr, itr1, itr2 ;
        long int days =0;
        long int val ;
        long int temp ;
        short int test = 0;
        


        for (int i=0; i<N; i++){
            scanf ("%ld",&val) ;
            s.insert (val) ;
        }   // inserting population of N countries
        
        
    /*     multiset <long int> :: iterator itrrr; 
    cout << "\nThe multiset gquiz1 is : "; 
    for (itrrr = s.begin(); itrrr != s.end(); ++itrrr) 
    { 
        cout << '\t' << *itrrr; 
    } 
    cout << endl; 
    */
    
        while (test == 0){
            // <0>  if set is empty 
            if (s.size() == 0){               // <0.1>
                test = 1;
                break;
            }

            else {                          // <0.2>

                // <1>  
                if (days == 0) {            // <1.1>     if its the first day 
                    itr = s.end () ;
                    --itr ;
                    temp = *(itr) ;       
                    
                    if (  (temp - x)*2 > temp ) {
                        days++ ;
                    }   
                    else {
                        s.erase(temp);
                        s.insert( (temp -x)*2 ) ;
                        days++ ;
                    }   
                    test = 0 ;  
                    continue ;   
                }    //end of <1.1> 

                else {                      // <1.2>     if it isnt first day we will do rangesearch [x,2x]
                    long int Z = range_search(s,x) ;
                    itr = s.end();
                    --itr;
                    temp = *(itr) ;

                    // <2>

                    if (Z != -1) {           // <2.1>     if some value found using rangesearch
                        if (Z == temp) {     // when Z is equal to most populated country's value
                            days = days + s.size() ;
                            test = 0 ;
                            break;
                        }

                        else {
                            x = Z;
                            s.erase (Z) ;
                            days++ ;
                            test = 0 ;
                            continue;
                        }

                    }    // end of <2.1>

                    else {                  // <2.2>
                        x = x*2;

                        if (  (temp - x)*2 > temp){
                            s.insert(temp) ;

                        } 
                        else {
                            s.erase(itr) ;
                            s.insert (  (temp - x)*2) ;
                        }

                        days++ ;
                        test = 0 ;
                        continue ;
                                            
                    }    // end of <2.2>

                    
                }    // end of <1.2>   
            }    // end of <0.2>
            
        }  // end of while loop

        cout<<days<<endl ;



    }  // end of testcase loop
    return 0;
}
注意:-我不在乎我的逻辑是否在边缘情况或其他测试情况下给了我错误的答案,我特别专注于修复我的代码以仅执行我的逻辑,并且我需要帮助来修复我的代码错误,因为我没有得到正确的答案我的逻辑给我的答案在纸上(意味着我做了一些代码失误,可能是溢出或其他原因)
我的逻辑-我创建了一个多集,以升序存储每个国家/地区的人口。
1.在任何一天(可能是固化的第一天),如果可用的固化次数为X,
等于或大于多重集合中的最大元素(人口最多的国家),那么答案将是多重集合的大小(好像我们从最后一个开始,我们就可以在一天之内治愈每个国家)
2.如果X小于多重集的最后一个元素,则出现两种情况:
2.A)如果是治愈的第一天-那么只需使用X来治愈人口最多的国家(集合的最后一个元素),则无需更新X(第二天X保持不变)然后更新该国家的人口(加倍后),我们在其中使用了X种固化方法。然后将所需的总天数加1。
2.B)else(当不是第一天时)-然后我们进一步检查情况,因此出现了两种情况-
 2.B.a) we will search for a element in multiset within the range [x,2x] inclusively
        if a value found then we will use as many cures as the value of element returned 
        i.e size of population found within range [x,2x] let that population be P, 
        thus X = P (updating X for next day) and we remove that element from multiset 
        (because this country will be country cured on that day) and we increment the 
        total days required by one.



 2.B.b) else (when no such value in multiset exits in range [x,2x] ) then again we will 
        go to cure the country with largest population present. X will be updated to 2*X 
        (because today we can use 2*X cures to cure the most populated country), thus 
        X = X*2, we also update the population of that country after doubling at end of 
        the day.
注意:同样,在执行所有这些任务之前,我还要检查我的多集是否为空。
我还附带了示例测试用例的说明。
测试用例1:
当X = 1,N = 5且国家/地区的人口分别为10、20、30、40、50
enter image description here
测试用例2:
当X = 10,N = 3且国家/地区的人口分别为1、20、110
enter image description here

最佳答案

You don't have to write a huge and lengthy code


The logic is very simple for this problem


  1. You just have sort the input array and check the nearest close population number than x

  1. Then you should count the days by supplying vaccines which increases your daily output

  1. The root part of this problem is to figure out supplying vaccines with 2X increase in cases daily

逻辑

检查人口是偶数还是奇数
现在上面两种情况出现两种情况
  • x小于人口
  • 的一半
  • x大于或等于一半人口(这对于解决问题非常重要)

    对于第一种情况,您必须在该国家/地区提供疫苗,直到x不低于
    第二种情况
    发生这种情况时,您只需提供一半人口的疫苗,就可以使x倍
    它最终使x等于总体
    发生这种情况时,您可以删除该国家/地区的所有患者,并在当天结束时获得当前国家/地区人口的x倍
    这是相继发生的,不要忘记添加前几天或没有添加
    不到x的人口,他们到达我们国家后的一天就可以康复
    人口最多...;)
    这是给您的交流代码。希望您能理解
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long 
    #define f(i,u,w) for(ll i=u;i<w;i++)
    #define AI(u,w) ll u[w]; f(i,0,w)cin>>u[i]
    #define T ll t; cin>>t; while(t--)
    
    int main(){
        
    /*  freopen("input.txt", "r" , stdin);
        freopen("output.txt", "w" , stdout);  */
        
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        
        T{
            ll n,x,ans=0;
            cin>>n>>x;
            AI(a,n);
            sort(a,a+n);
            ll b=lower_bound(a,a+n,x)-a;
            ans=ans+b;
            if(b!=0){
                if(2*a[b-1]>x)
                x=2*a[b-1];
            }
            f(i,b,n){
                if(x>=a[i]){
                    ans++;
                    x=a[i]*2;
                }
                else{
                    ll c=(a[i]+1)/2;
                    if(x>=c){
                        ans+=2;
                        x=a[i]*2;
                    }
                    else{
                        ans++;
                        while(x<c){
                            x=x<<1;
                            ans++;
                        }
                        ans++;
                        x=a[i]*2;
                    }
                }
            }
            cout<<ans<<"\n";
        }
    }
    

  • 关于c++ - 需要帮助来修复我的代码以执行特定的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63312634/

    相关文章:

    c++ - sfinae 远离破坏者

    java - 从最频繁的 X 条目的多重集中获取有序子集

    c++ - 从 QTreeView 中删除项目时取消选择所有行

    c++ - 如何检查嵌套模板的类型?

    c++ - 为什么 boost::adaptors::filtered 的输出没有名为 size 的成员?

    c++ - 现在允许重新定义 constexpr 静态数据成员吗? (但不是内联常量)?

    SQL Informix 12.10FC6 - 一次转换多列多列,但不创建用户定义类型

    c++ - 定义映射/集合时如何实例化比较函数(仿函数)?

    c++ - 如何测试类型转换时间?

    python - pybind 如何操作 py::list 对象