再求题

灌水区

@[lucy2012](/user/1252442) 不是,您代码呢? 看起来应该是贪心
by KobeBeanBryantCox @ 2024-04-21 14:21:54


```cpp #include<bits/stdc++.h> using namespace std; int n,a,b,c,d,sum,num1,num2; int main(){ cin>>n; for(int i=1;i<=n;i++){ sum=num1=num2=0; cin>>a>>b>>c>>d; while(a--){ sum++;num1++;num2++; } while(num1>=0&&num2>=0){ if(num1<num2){ if(b>0){ b--;num1++;num2--;sum++; }else if(c>0){ c--;num1--;num2++;sum++; }else break; }else{ if(c>0){ c--;num1--;num2++;sum++; }else if(b>0){ b--;num1++;num2--;sum++; }else break; } } while(d--){ if(b<0||c<0) break; num1--;num2--;sum++; } cout<<sum<<endl; } return 0; } ``` @[KobeBeanBryantCox](/user/865625) 不好一丝哈
by lucy2012 @ 2024-04-21 14:31:34


@[lucy2012](/user/1252442) `if(b<0||c<0)`这一行 改成`if(num1<0||num2<0)`
by KobeBeanBryantCox @ 2024-04-21 14:36:41


@[KobeBeanBryantCox](/user/865625) 谢谢啦!不过tle有没有可以优化的地方?QwQ
by lucy2012 @ 2024-04-21 14:39:22


@[lucy2012](/user/1252442) 您这个肯定可以优化的 比如 ```cpp while(a--){ sum++;num1++;num2++; } ``` 可以改成 ```cpp sum+=a,num1+=a,num2+=a,a=0; ``` 还有很多地方都可以优化
by KobeBeanBryantCox @ 2024-04-21 14:41:47


@[KobeBeanBryantCox](/user/865625) 玄学,改这样从60变成了50 QwQ ```cpp #include<bits/stdc++.h> using namespace std; int n,a,b,c,d,sum,num1,num2; int main(){ cin>>n; for(int i=1;i<=n;i++){ sum=num1=num2=0; cin>>a>>b>>c>>d; sum+=a;num1+=a;num2+=a;a=0; while(num1>=0&&num2>=0){ if(num1<num2){ if(b>0){ b--;num1++;num2--;sum++; }else if(c>0){ c--;num1--;num2++;sum++; }else break; }else{ if(c>0){ c--;num1--;num2++;sum++; }else if(b>0){ b--;num1++;num2--;sum++; }else break; } } while(d--){ if(num1<0||num2<0) break; num1--;num2--;sum++; } cout<<sum<<endl; } return 0; } ```
by lucy2012 @ 2024-04-21 14:45:48


@[lucy2012](/user/1252442) 改过后的代码 ```cpp #include<bits/stdc++.h> using namespace std; int n,a,b,c,d,sum,num1,num2; int main(){ cin>>n; for(int i=1;i<=n;i++){ sum=num1=num2=0; cin>>a>>b>>c>>d; sum+=a,num1+=a,num2+=a; int t=min(b,c); b-=t,c-=t,sum+=2*t; if(b>0) if(num1-b>-1)num1-=b,sum+=b,num2+=b; else sum+=num1+1,num2+=num1+1,num1=-1; else if(num2-c>-1)num2-=c,sum+=c,num1+=b; else sum+=num2+1,num1+=num2+1,num2=-1; sum+=min(d,min(num1,num2)+1); cout<<sum<<endl; } return 0; } ```
by KobeBeanBryantCox @ 2024-04-21 14:53:27


@[KobeBeanBryantCox](/user/865625) ok,非常感谢啦!
by lucy2012 @ 2024-04-21 14:54:38


^w^o
by lucy2012 @ 2024-04-21 14:55:18


|