codeforces 344 B. Print Check解题报告

来源:互联网 发布:手机视频搞怪软件 编辑:程序博客网 时间:2024/06/05 09:30

B. Print Check
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Kris works in a large company “Blake Technologies”. As a best engineer of the company he was assigned a task to develop a printer that will be able to print horizontal and vertical strips. First prototype is already built and Kris wants to tests it. He wants you to implement the program that checks the result of the printing.
Printer works with a rectangular sheet of paper of size n × m. Consider the list as a table consisting of n rows and m columns. Rows are numbered from top to bottom with integers from 1 to n, while columns are numbered from left to right with integers from 1 to m. Initially, all cells are painted in color 0.
Your program has to support two operations:
1. Paint all cells in row ri in color ai;
2. Paint all cells in column ci in color ai.
If during some operation i there is a cell that have already been painted, the color of this cell also changes to ai.
Your program has to print the resulting table after k operation.
Input
The first line of the input contains three integers n, m and k (1  ≤  n,  m  ≤ 5000, n·m ≤ 100 000, 1 ≤ k ≤ 100 000) — the dimensions of the sheet and the number of operations, respectively.
Each of the next k lines contains the description of exactly one query:
• 1 ri ai (1 ≤ ri ≤ n, 1 ≤ ai ≤ 109), means that row ri is painted in color ai;
• 2 ci ai (1 ≤ ci ≤ m, 1 ≤ ai ≤ 109), means that column ci is painted in color ai.
Output
Print n lines containing m integers each — the resulting table after all operations are applied.
Examples
input
3 3 3
1 1 3
2 2 1
1 2 2
output
3 1 3
2 2 2
0 1 0
input
5 3 5
1 1 1
1 3 1
1 5 1
2 1 1
2 3 1
output
1 1 1
1 0 1
1 1 1
1 0 1
1 1 1
Note
The figure below shows all three operations for the first sample step by step. The cells that were painted on the corresponding step are marked gray.

这里写图片描述

——————–人笨就该多写解题报告,将每题研究透————————-
题意:有n行m列的矩阵,初始值都是0。给k次操作,操作有两种,操作1表示将某行全部元素变成某个数;操作2表示将某列全部变成某个数。打印最后的矩阵。

思考:模拟肯定超时。矩阵某个点可能会被改变很多次,后面改变的值会覆盖前面的值,因此打印值是最后改变的那个值。那么就找个点最后的改变值。对点( i , j ),只有操作(1,i)和操作(2,j),那么两个操作中后出现的那个决定( i ,j)的值。
程序有数据结构和算法组成,因此考虑算法和数据结构:
1、 数据结构:结构要存的信息有操作数(1或2)、行号或列号、改变值、还有出现的顺序值。考虑到对点( i , j ),只有操作(1,i)和操作(2,j),可以建一个二维数组Move[i][j],i表示操作数,j表示行或列。还有改变值和出现顺序没有保存,因此可以用一个结构体来存,那么Move类型就是结构体。
2、 算法:对点( i , j ),看操作(1,i)和操作(2,j)是否对其有改变,有的话输出最后改变的那个值,都没有的话输出0。

//124ms,2400KB#include <stdio.h>#include <fstream>#include <string.h>#include<iostream> using namespace std; struct{    int val=0;    int order=0;}Move[3][100005];int order=0;int main(){    freopen("i.txt","r",stdin);    int n,m,k,tmp1,tmp2;    cin>>n>>m>>k;     for(int i=0;i<k;i++) {        scanf("%d%d",&tmp1,&tmp2);        scanf("%d",&Move[tmp1][tmp2].val);        Move[tmp1][tmp2].order=order++;//保存输入顺序    }     for(int i=1;i<=n;i++){        for(int j=1;j<=m;j++){            if(Move[1][i].val==0&&Move[2][j].val==0)cout<<0<<" ";//如果点没有被改变            else if(Move[2][j].val==0||Move[1][i].order>Move[2][j].order)cout<<Move[1][i].val<<" ";// Move[1][i]最后操作            else cout<<Move[2][j].val<<" ";// Move[2][j]最后操作        }        cout<<endl;    }    return 0;}

看来官方题解,才知道还可以更好:
官方解题:紧紧围绕“记录改变点(I,j)值的最后一个操作”这句话。
比我的程序优越的地方:1、省了内存。没有单独的记录出现顺序的变量order,我的程序里这个变量也是多于的。2、省时。少了频繁的if条件语句的判断。3、一切都是因为数据结构的选取。因为我选的是结构体,改变值和输入顺序在同一个结构里面,因此要先根据输入顺序找到结构体,再从该结构体里找到改变值。然而官方题解中顺序和改变值是分开存放的,灵活性更大。

//78ms,240KB#include <cstdio>#include <algorithm>const int N = 5000, K = (int)1e5 + 1;int n, m, k,t, p,l[N], c[N], x[K];int main() {    scanf("%d%d%d", &n, &m, &k);    x[0] = 0;    for (int i = 1; i <= k; ++i) {        scanf("%d%d%d", &t, &p, &x[i]);        if (t == 1) l[--p] = i;        else c[--p] = i;    }     for (int i = 0; i < n; ++i) {        for (int j = 0; j < m; ++j)            printf("%d ", x[std::max(l[i], c[j])]);        putchar('\n');    }    return 0;}
0 0
原创粉丝点击