当前位置:   article > 正文

Codeforces Round 872 (Div. 2) 题解_c. luotianyi and the show

c. luotianyi and the show

总结:5.9有同学问B题,所以就连同ABC题一起做了,都算是思维题吧,难度不算高

A. LuoTianyi and the Palindrome String

思路:输入的都为回文字符串,如果输入的回文字符串每个字符都相同,如"aaaaa",无论删去哪几个,都还是回文字符串,所以输出-1,若为其他情况,只需删去第一个字符便一定不是回文字符串,所以输出字符串的长度-1即可

  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4. int T;
  5. string s;
  6. bool f;
  7. cin>>T;
  8. while(T--){
  9. f=0;
  10. cin>>s;
  11. for(int i=1;i<=s.size()-1;i++){
  12. if(s[i]!=s[i-1]){
  13. f=1;
  14. break;
  15. }
  16. }
  17. if(!f){
  18. cout<<"-1"<<'\n';
  19. }else{
  20. cout<<s.size()-1<<'\n';
  21. }
  22. }
  23. return 0;
  24. }

B. LuoTianyi and the Table

当时做这个题的时候开始只考虑了一种情况,即第一个数为最大值,周围为最小值,考虑了一会儿才想到另一种情况

思路:

以样例

2 3

7 8 9 -3 10 8

为例:

        第一种情况第一个数为最大值:

 

        第二种情况,第一个数为最小值:

 只需考虑第一个数和他周围的两个数即可,和第一个数相差最大的放到边长较长的一侧,即(a[n*m]-a[1])*(max(n,m)-1)*min(n,m),最大值和最小值的差*较长的边减1*较短的边,另一部分(a[n*m]-a[2])*(min(n,m)-1),最大值和第二小值的差*较短的边减一,另一种情况同理,两种情况求最大值

  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. const int N=200005;
  5. int a[N];
  6. int main(){
  7. int T,n,m;
  8. cin>>T;
  9. while(T--){
  10. cin>>n>>m;
  11. for(int i=1;i<=n*m;i++){
  12. cin>>a[i];
  13. }
  14. sort(a+1,a+1+n*m);
  15. cout<<max((a[n*m]-a[1])*(max(n,m)-1)*min(n,m)+(a[n*m]-a[2])*(min(n,m)-1),(a[n*m]-a[1])*(max(n,m)-1)*min(n,m)+(a[n*m-1]-a[1])*(min(n,m)-1))<<'\n';
  16. }
  17. return 0;
  18. }

C. LuoTianyi and the Show

开始确实没有想到这个做法

思路:考虑三种情况,只考虑-1,只考虑-2,或者三种都考虑,因为是最左边的左边和最右边的右边,所以基准点左边考虑-1,基准点右边考虑-2,先对固定位置的进行排序,然后逐个进行计算,注意最大可以容纳的人数,不要超过

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstring>
  4. using namespace std;
  5. const int N=100005;
  6. int a[N],qsum[N],hsum[N];
  7. bool vis[N];
  8. int main(){
  9. int T,n,m,c,d,x,cnt,ans;
  10. cin>>T;
  11. while(T--){
  12. memset(a,0,sizeof(a));
  13. memset(vis,0,sizeof(vis));
  14. c=0;d=0;cnt=0;ans=0;
  15. cin>>n>>m;
  16. for(int i=1;i<=n;i++){
  17. cin>>x;
  18. if(x==-1){
  19. c++;
  20. }else if(x==-2){
  21. d++;
  22. }else{
  23. if(!vis[x]){
  24. a[++cnt]=x;
  25. vis[x]=1;
  26. }
  27. }
  28. }
  29. sort(a+1,a+1+cnt);
  30. ans=min(m,c+cnt);
  31. ans=max(ans,min(m,d+cnt));
  32. for(int i=1;i<=cnt;i++){
  33. ans=max(ans,min(a[i],c+i)+min(m-a[i],d+cnt-i));
  34. }
  35. cout<<ans<<'\n';
  36. }
  37. return 0;
  38. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/519153
推荐阅读
相关标签
  

闽ICP备14008679号