swjtu2381(Matrix)

来源:互联网 发布:常见文本文件格式知乎 编辑:程序博客网 时间:2024/06/05 05:31

题目链接:http://swjtuoj.cn/problem/2381/

直接暴力的话,总得时间复杂度是O(T*n*q)的,TLE。

虽然有10^5次操作,但是矩阵大小只有500*500,而且对于同一行(列)的操作,只有最后一次有效,只保留最后一次操作就好,那么最后剩下的最多也就1000次操作,然后按照输入时的相对先后顺序暴力一次就好。

#include <iostream>#include <cstdio>#include <memory.h>#include <algorithm>using namespace std;struct node{    int op, col, p;//p是输入顺序    int x;    node() {op = col = p = x = 0;}    node(int o, int x, int c, int p):op(o), x(x), col(c), p(p) {}};node H[510];//保留行操作node L[510];//保留列操作node b[1020];int a[510][510];int cmp(const node& a, const node& b){    return a.p < b.p;}int main(){    int t;    scanf("%d", &t);    for (int _=0; _<t; _++)    {        int n, q;        scanf("%d%d",&n,&q);        for (int i=1; i<=n; i++)            for (int j=1; j<=n; j++) a[i][j] = 0;        for (int i=1; i<=n; i++) H[i] = L[i] = node();        for (int i=0; i<q; i++)        {            int o, x, c;            scanf("%d%d%d",&o, &x, &c);            if (o == 1)                H[x] = node(o, x, c, i+1);            else                L[x] = node(o, x, c, i+1);        }        int cnt = 0;        for (int i=1; i<=n; i++)            if (H[i].x != 0) b[cnt++] = H[i];        for (int i=1; i<=n; i++)            if (L[i].x != 0) b[cnt++] = L[i];        sort(b,b+cnt, cmp);        for (int i=0; i<cnt; i++)        {            if (b[i].op == 1)                for (int j=1; j<=n; j++) a[b[i].x][j] = b[i].col;            else                for (int j=1; j<=n; j++) a[j][b[i].x] = b[i].col;        }        for (int i=1; i<=n; i++)        {            for (int j=1; j<n; j++)                printf("%d ", a[i][j]);            printf("%d\n", a[i][n]);        }    }    return 0;}


原创粉丝点击