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);
}
}