Codeforces Round 879 (Div. 2)前三题答案

因为我本人比较菜,所以做不出来几个题

1.首先看第一个题

思路分析

每个样例呢会给你许多个1和-1;

要保证-1的个数为偶数并且-1的个数大于1的个数。

所以先选一个极限的-1的个数,个数d=总数n/2;

如果这个d为奇数那么就要减去1,因为如果有6个数字,d=6/2=3;d加一会导致总和<0;

接下来

如果-1的个数本就比d小,那么看-1的个数为奇数还是偶数,奇数的话就输出1,因为只需要把任意一个-1变为1即可。

如果-1的个数大于d

那么就输出d减去-1的个数

代码如下

import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s1 = reader.readLine();
        int t=Integer.parseInt(s1);
        while(t-->0){
            String s2= reader.readLine();
            int n =Integer.parseInt(s2);
            String s3[]=reader.readLine().split(" ");
            int count1=0;
            int count2=0;
            for(int i=0;i<n;i++){
                int shu=Integer.parseInt(s3[i]);
                if(shu==1)count1++;
                else count2++;
            }
            int d=n/2;
            if(d%2!=0)d=d-1;
            if(count2==0)System.out.println("0");
            else
            if(d>=count2&&count2%2==0)System.out.println("0");
            else {
                
                if(d>count2){
                    System.out.println("1");
                }else {
                    System.out.println(Math.abs(d-count2));
                }
            }
        }
    }
    
}

完美通过

 

2.接下来看第二个题目

这个就模拟

如果a串b串相等,那就就直接输出0

不然的话就判断二者长度是否相等然后就是算.。

import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s1 = reader.readLine();
        int t=Integer.parseInt(s1);
        while(t-->0){
            String s2[]=reader.readLine().split(" ");
            String a=s2[0];
            String b=s2[1];
            long ans = 0l;
            int n = a.length(), m = b.length();
            int l = 0;
            if (a.equals(b))
            {
                System.out.println("0");
                continue;
            }
            else if (n == m)
            {
                while(l < n && a.charAt(l) == b.charAt(l)) l++;
                int p = (a.charAt(l) - '0');
                int q = (b.charAt(l) - '0');
                ans += Math.abs(p - q);
                ans += (n - l - 1) * 9;
            }
            
            else
            {
                if (n > m) ans += (a.charAt(0) - '0');
                else ans += (b.charAt(0) - '0');
                ans += Math.max(n - 1, m - 1) * 9;
            }
            System.out.println(ans);
        }
    }
}

3.第三题

这个题是一个博弈论

一般遇到博弈论问题,就去找寻关键信息,然后多列几个例子来试试就出答案了

这个题目的关键信息是将两个字符串正着匹配和倒着匹配的个数

如果正着来,不匹配的个数和所需要最小的调整数为

正向不匹配的个数  0  1  2  3  4  5  6

所需要的最小数     0  1  4   5  8  9  12

正向不匹配的个数  0  1  2  3  4  5  6

所需要的最小数     2   2  3  6   7 10 11

很容易就可以找到规律

然后将二者的最小值输出即可 

import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s1 = reader.readLine();
        int t=Integer.parseInt(s1);
        while(t-->0){
            String s2= reader.readLine();
            int n =Integer.parseInt(s2);
            String s3=reader.readLine();
            String s4 =reader.readLine();
            int qian=0;
            int hou=0;
            char r1[]=s3.toCharArray();
            char r2[]=s4.toCharArray();
            for(int i=0;i<n;i++){
                if(r1[i]!=r2[i])qian++;
                if(r1[i]!=r2[n-1-i])hou++;
            }
            int all1=0;
            int all2=2;
            int ans=0;
            if(qian==0)all1=0;
            else
            if(qian>=2)
            all1+=(qian/2)*4+qian%2;
            else all1=1;
 
            if(hou==0||hou==1)all2=2;
            else 
            if(hou>=2){
                all2+=((hou-1)/2)*4+(hou-1)%2;
            }
            System.out.println(Math.min(all1, all2));
        }
    }
}

提交完美通过

 

 

 

  1.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值