Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 给定3个正整数,求最大步数,将其减为0_C++_Algorithm_Logic - Fatal编程技术网

C++ 给定3个正整数,求最大步数,将其减为0

C++ 给定3个正整数,求最大步数,将其减为0,c++,algorithm,logic,C++,Algorithm,Logic,我正在解决来自CodeForces的问题: 我们得到了三个值,r、g和b,分别代表三堆红色、绿色和蓝色糖果的数量。Tanya不能一天吃两个相同颜色的糖果,也就是说,她每天只吃两个不同颜色的糖果。找出Tanya可以吃糖果的最大天数 我用一种简单的方式做了如下: int main() { int t, r, g, b; cin>>t; while(t--) { int counter=0; cin >> r >&

我正在解决来自CodeForces的问题:

我们得到了三个值,
r
g
b
,分别代表三堆红色、绿色和蓝色糖果的数量。Tanya不能一天吃两个相同颜色的糖果,也就是说,她每天只吃两个不同颜色的糖果。找出Tanya可以吃糖果的最大天数

我用一种简单的方式做了如下:

int main() {
    int t, r, g, b;
    cin>>t;

    while(t--) {
        int counter=0;
        cin >> r >> g >> b;

        while(r && g) {
            r--;
            g--;
            counter++;
        }

        while(g && b) {
            g--;
            b--;
            counter++;
        }

        while(r && b) {
            r--;
            b--;
            counter++;
        }

        cout<<counter<<"\n";
    }

    return 0;
}
intmain(){
int t,r,g,b;
cin>>t;
而(t--){
int计数器=0;
cin>>r>>g>>b;
while(r&g){
r--;
g--;
计数器++;
}
while(g&b){
g--;
b--;
计数器++;
}
while(r&b){
r--;
b--;
计数器++;
}

因为你需要找到解决这个问题的最佳方法

例如,1,1,10,最佳方法是r&b,g&b。在您的方法中,结果仅为1


因此,我们需要对这三个值进行排序,并始终使用最大的数值来获得答案。

即使您的解决方案得到纠正,它也可能无法通过对代码力的所有测试,因为它的时间复杂度与您输入的数值成正比。但是,存在一个解决方案,该解决方案的运行时间是恒定的,而与输入值无关输入数字

首先,对输入的数字进行排序。
3
如果没有对它们进行排序,那么在每次迭代中,我们需要找到最大和最小元素,然后对它们进行递减。如果对数字进行排序,那么我们立即知道最大和最小数字的位置,因此我们可以立即递减它们

排序后,让我们考虑<代码> A、B、C可能的情况:A<代码> 1,1,2< /代码> ->代码> 0,1,1/代码> ->代码> 0,0,0< /代码> ./P> 2.对于
a,b,c:a!=b和b!=c
我们应该记住最大化迭代次数的大小写
1
。如何达到这一点?好吧,只要
a
变为
0
(那么不需要case
1
,因为我们已经可以将剩余的步骤计算为
min(b,c-a)
)或者
c
变为等于
b
,然后情况是
1
。如果我们尝试减少任何其他数字对,那么迭代次数将减少,因为
b
将无缘无故地减少:)。在此之后,我们可以应用case
1

3.这种方法可以在
O(1)
时间复杂度中实现

...    

int main() {
    int t;
    std::cin >> t;

    for (int i = 0; i < t; i++) {
        std::vector<int32_t> array(3);
        for (int32_t& value : array) {
            std::cin >> value;
        }
        std::sort(array.begin(), array.end());
        int32_t days = 0;

        int32_t diff = array[2] - array[1];
        days += (std::min(array[0], diff));
        array[0] -= days;
        array[2] -= days;

        array[2] -= array[0] / 2;
        days += (array[0] / 2);

        array[1] -= array[0] / 2;
        days += (array[0] / 2);

        days += std::min(array[1], array[2]);

        std::cout << days << std::endl;
    }

    return 0;
}

...
。。。
int main(){
int t;
标准:cin>>t;
for(int i=0;i>值;
}
排序(array.begin(),array.end());
int32天=0;
int32_t diff=数组[2]-数组[1];
天数+=(标准::分钟(数组[0],差异));
数组[0]=天;
数组[2]=天;
数组[2]-=数组[0]/2;
天数+=(数组[0]/2);
数组[1]-=数组[0]/2;
天数+=(数组[0]/2);
天数+=std::min(数组[1],数组[2]);
标准::cout