CSP 201909

CSP 中Java 时间为CPP的十倍, 内存比CPP多256MB

201909-1 小明种苹果

简单暴力模拟,不解释

import java.util.Scanner;

public class Main{

    private static long ans = 0;

    public static void main(String arg[]){
        Scanner in = new Scanner(System.in);

        long maxx = -1;
        int index = -1;
        int n,m;
        n = in.nextInt();
        m = in.nextInt();

        for(int i=0; i<n; i++){
            long apple_amount = 0;
            ans += in.nextInt();
            for(int j=0; j<m; j++){
                apple_amount += (-1*in.nextInt());
            }
            if(maxx < apple_amount){
                maxx = apple_amount;
                index = i+1;
            }
            ans -= apple_amount;
        }
        in.close();

        System.out.printf("%d %d %d\n",ans, index, maxx);

    }
}

在这里插入图片描述

201909-1 小明种苹果(续)

仍然是简单暴力模拟,,不解释,,,
鄙人犯了一个很傻的错误,结果调了一个小时,,,emmmmmm。
我把三个连续树的判断放到了对每棵树的每个操作中去了,即放到了(j循环,ans!=number的if中)这样会有重复计数,,,(因为每一棵树可以多次掉果,,)
简直了,,,,

import java.util.Scanner;

public class Main{
    private static boolean first = false,second = false,pre1 = false,pre2 = false,cur = false;


    public static void main(String[] arg){
        Scanner in = new Scanner(System.in);
        int size = in.nextInt();

        long summ = 0;     // all apple
        int tree_size = 0; // tree with fall apple
        int times = 0;     //connected tree

        //for each tree
        for(int i=0; i<size; i++){

            int t_size = in.nextInt();
            int ans = -1;
            int number = -1;

            //for each operator in each tree
            for(int j=0; j<t_size; j++){
                number = in.nextInt();
                if(number > 0){
                      if(j != 0 && ans != number) cur = true;
                      ans = number;
                }else{
                    ans += number;
                }
            }

            // judge connected tree
            if(cur == true) {
                tree_size ++;
                if(pre1 == true && pre2 == true) times++;
                if(i == size - 1) {
                    if (pre1 == true && first == true) times++;
                    if (first == true && second == true) times++;
                }
                if(i == 0) first = true;
                if(i == 1) second = true;
            }

            //update previous status
            summ += ans;
            pre2 = pre1;
            pre1 = cur;
            cur = false;
        }
        in.close();
        System.out.printf("%d %d %d\n",summ, tree_size, times);
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值