优质奶牛 差分数组实现多次修改数组

[编程题]优质奶牛

时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 64M,其他语言128M

牛牛管理这一片牧场,在这片牧场中,一共有 头奶牛,为方便统计,它们排成了一排,编号为 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();
    }
}
经验分享 程序员 微信小程序 职场和发展