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;}
阅读全文
0 0
- swjtu2381(Matrix)
- 《Matrix》
- Matrix
- Matrix
- Matrix
- Matrix
- Matrix
- Matrix
- mAtrix
- Matrix
- matrix
- Matrix
- Matrix
- Matrix
- Matrix
- Matrix
- Matrix
- Matrix
- 标准C++中的string类的用法总结
- PID的快速理解与掌握
- 函数中的数组传递
- BZOJ 2306 [Ctsc2011] 幸福路径 [倍增+floyd做法]
- 机器学习的相关模块安装:pandas,numpy, scipy,scikit_learn(sklearn), matplotlib在winows系统下的安装问题
- swjtu2381(Matrix)
- Java过滤器与SpringMVC拦截器之间的关系与区别
- Hbase<踩过的坑--使用intellij idea调用JavaAPI创建预分区>
- javascript中的标签语句label:statement
- Rocket简介(1)
- Java的数据类型
- BZOJ 4720 [Noip2016]换教室——期望DP
- wamp多版本php的配置
- Factorial vs Power