Rinne Loves Study

鏈接:https://ac.nowcoder.com/acm/contest/370/C
來源:牛客網

Rinne Loves Study
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
Rinne 喜歡使用一種奇怪的方法背單詞,現在這些單詞被放在了一個
n
×
m
n×m 的格子裏。由於背單詞是一個令人煩躁的事情,所以她決定每天只背同一行或者同一列的單詞。她一共會背 T 次單詞,爲了方便鞏固,她現在想知道:對於每個單詞,最後一次背是什麼時候呢?
她這麼可愛當然會算啦!但是她想考考你。
輸入描述:
第一行三個整數 n,m,T。
接下來 T 行,第 i+1 行描述第 i 天干了什麼,每行的格式如下:
1 x:說明她在這一天背了第 x 行的單詞;
2 y說明她在這一天背了第 y 列的單詞。
輸入的所有量的具體意義請參考「題目描述」。
輸出描述:
輸出一個
n
×
m
n×m 的矩陣 a,
a
i
,
j
ai,j 表示第 i 行第 j 列這個單詞最後一次被背誦是在第幾天。
示例1
輸入
複製
3 3 3
1 2
2 3
1 3
輸出
複製
0 0 2
1 1 2
3 3 3
備註:
n
×
m

10
5
,
T

10
5

思路:剛開始的時候是打算直接模擬就行,因爲思路很容易就出了,所以沒注意到數據的範圍,所以後來改了一下,換成兩個一維數組來存儲數據,時間複雜度就降了下去。

超時代碼O(nmt):

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int t=sc.nextInt();
        int [][]num=new int[n][m];
        int time=1;
        int tmp=0;
        int index=0;
        int i=0;int j=0;
        while(t>0){
            tmp=sc.nextInt();
            index=sc.nextInt();
            if(tmp==1)
                for(i=0;i<m;i++)
                    num[index-1][i]=time;
            else
                for(i=0;i<n;i++)
                    num[i][index-1]=time;
            time++;
            t--;
        }
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                System.out.print(num[i][j]+" ");
            }
            System.out.println();
        }
        sc.close();
    }
}

AC代碼O(t):

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int t=sc.nextInt();
        int []hang=new int[n];
        int []lie=new int[m];
        int time=1;
        while(t>0){
            int tmp=sc.nextInt();
            int index=sc.nextInt();
            if(tmp==1)
                hang[index-1]=time;
            else
                lie[index-1]=time;
            time++;
            t--;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                System.out.print(Math.max(hang[i],lie[j])+" ");
            }
            System.out.println();
        }
        sc.close();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章