[编程题]优质奶牛
牛牛管理这一片牧场,在这片牧场中,一共有 头奶牛,为方便统计,它们排成了一排,编号为 1~n 。
现在质检员牛妹在检测这些奶牛的质量,她列出了 条特性,只有满足所有特性的奶牛才可称之为优质奶牛。
但是,牛牛现在只知道对于某条特性,某几段连续区间内的奶牛是满足条件的,如果这样依次排查,会浪费很多时间。由于牛妹时间很急,马上要赶往下一个牧场,所以,牛牛请你帮助他筛选优质奶牛。
输入例子1:
1
10 2
3
1 2
4 5
8 8
2
1 4
6 8
输出例子1:
4
1 2 4 8
差分数组实现多次修改数组,差分(区间修改求最小值高效)
很变态,必须用流输入输出才符合运行时间,其实这道题出的有点问题
import java.io.*;
import java.util.*;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static void main(String[] args) throws IOException {
int t = nextInt();
while(t-->0){
int n = nextInt(), m = nextInt();
//每头牛符合的条件数量
int[] count = new int[n+1];
boolean[][] matrix = new boolean[m][n];
for (int j = 0; j < m; j++) {
int[] now = new int[n+2];
int k = nextInt();
while(k-->0){
int l = nextInt(), r = nextInt();
now[l]++;
now[r+1]--;
}
//还原
for(int i=1; i<=n; i++){
now[i]+=now[i-1];
}
//判断达标
for(int i=1; i<=n; i++){
if(now[i]>=1)
count[i]++;
}
}
List<Integer> list = new ArrayList<>();
for (int i=1; i<=n; i++) {
if(count[i]==m){
list.add(i);
}
}
out.println(list.size());
for (int cow : list) {
out.print(cow+" ");
}
out.println();
}
out.flush();
}
}