【链表】【模拟】Codeforces 706E Working routine
题目链接:
http://codeforces.com/problemset/problem/706/E
题目大意:
给一个N*M的矩阵,Q个操作,每次把两个同样大小的子矩阵交换,子矩阵左上角坐标分别为(a,b)和(c,d),高度h,宽度w。
(2 ≤ n, m ≤ 1000, 1 ≤ q ≤ 10 000)
题目思路:
【链表】【模拟】
这一看如果直接模拟的话时间复杂度是N*M*Q,肯定T了。
把矩阵看成链表,链表的方向有上下左右四种,其实每次交换两个子矩阵只改变的外围一圈的链表值,而内部的链接是没有变的,所以可以按照链表做。
每次交换的时候,把子矩阵的最上、下、左、右的边的相应需要改变的链表值和另一个矩阵交换,这样就把两维问题转化为一维。
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 1004
#define M 10004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
struct xxx
{
int x,y;
}shang[N][N],xia[N][N],zuo[N][N],you[N][N];
int a[N][N];
void changeshang(int x1,int y1,int x2,int y2)
{
xxx s1,s2;
s1=shang[x1][y1];
s2=shang[x2][y2];
xia[s1.x][s1.y].x=x2,xia[s1.x][s1.y].y=y2;
xia[s2.x][s2.y].x=x1,xia[s2.x][s2.y].y=y1;
shang[x1][y1]=s2;
shang[x2][y2]=s1;
}
void changexia(int x1,int y1,int x2,int y2)
{
xxx s1,s2;
s1=xia[x1][y1];
s2=xia[x2][y2];
shang[s1.x][s1.y].x=x2,shang[s1.x][s1.y].y=y2;
shang[s2.x][s2.y].x=x1,shang[s2.x][s2.y].y=y1;
xia[x1][y1]=s2;
xia[x2][y2]=s1;
}
void changezuo(int x1,int y1,int x2,int y2)
{
xxx s1,s2;
s1=zuo[x1][y1];
s2=zuo[x2][y2];
you[s1.x][s1.y].x=x2,you[s1.x][s1.y].y=y2;
you[s2.x][s2.y].x=x1,you[s2.x][s2.y].y=y1;
zuo[x1][y1]=s2;
zuo[x2][y2]=s1;
}
void changeyou(int x1,int y1,int x2,int y2)
{
xxx s1,s2;
s1=you[x1][y1];
s2=you[x2][y2];
zuo[s1.x][s1.y].x=x2,zuo[s1.x][s1.y].y=y2;
zuo[s2.x][s2.y].x=x1,zuo[s2.x][s2.y].y=y1;
you[x1][y1]=s2;
you[x2][y2]=s1;
}
void print()
{
int i,j,x,y,ii,jj;
for(i=;i<=n;i++)
{
x=you[i][].x;y=you[i][].y;
for(j=;j<=m;j++)
{
printf("%d ",a[x][y]);
ii=you[x][y].x;
jj=you[x][y].y;
x=ii;y=jj;
}
puts("");
}
puts("");
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int X,Y,XX,YY,x,y,xx,yy,ii,jj,aa,bb,cc,dd,ww,hh;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
while(~scanf("%d",&n))
{
mem(shang,);mem(xia,);mem(zuo,);mem(you,);
scanf("%d%d",&m,&cas);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%d",&a[i][j]);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
xia[i][j].x=i+,xia[i][j].y=j,you[i][j].x=i,you[i][j].y=j+;
for(i=;i<=n+;i++)
for(j=;j<=m+;j++)
shang[i][j].x=i-,shang[i][j].y=j,zuo[i][j].x=i,zuo[i][j].y=j-;
for(k=;k<=cas;k++)
{
scanf("%d%d%d%d%d%d",&aa,&bb,&cc,&dd,&hh,&ww); X=you[aa][].x,Y=you[aa][].y;
for(i=;i<bb;i++)
{
ii=you[X][Y].x,jj=you[X][Y].y;
X=ii,Y=jj;
}
XX=you[cc][].x,YY=you[cc][].y;
for(i=;i<dd;i++)
{
ii=you[XX][YY].x,jj=you[XX][YY].y;
XX=ii,YY=jj;
}
//==================
x=X,y=Y,xx=XX,yy=YY;
for(i=;i<ww;i++)
{
changeshang(x,y,xx,yy);
ii=you[x][y].x,jj=you[x][y].y;
x=ii,y=jj;
ii=you[xx][yy].x,jj=you[xx][yy].y;
xx=ii,yy=jj;
}
//==================
x=X,y=Y,xx=XX,yy=YY;
for(i=;i<hh;i++)
{
ii=xia[x][y].x,jj=xia[x][y].y;
x=ii,y=jj;
ii=xia[xx][yy].x,jj=xia[xx][yy].y;
xx=ii,yy=jj;
}
for(i=;i<ww;i++)
{
changexia(x,y,xx,yy);
ii=you[x][y].x,jj=you[x][y].y;
x=ii,y=jj;
ii=you[xx][yy].x,jj=you[xx][yy].y;
xx=ii,yy=jj;
}
//==================
x=X,y=Y,xx=XX,yy=YY;
for(i=;i<hh;i++)
{
changezuo(x,y,xx,yy);
ii=xia[x][y].x,jj=xia[x][y].y;
x=ii,y=jj;
ii=xia[xx][yy].x,jj=xia[xx][yy].y;
xx=ii,yy=jj;
}
//==================
x=X,y=Y,xx=XX,yy=YY;
for(i=;i<ww;i++)
{
ii=you[x][y].x,jj=you[x][y].y;
x=ii,y=jj;
ii=you[xx][yy].x,jj=you[xx][yy].y;
xx=ii,yy=jj; }
for(i=;i<hh;i++)
{
changeyou(x,y,xx,yy);
ii=xia[x][y].x,jj=xia[x][y].y;
x=ii,y=jj;
ii=xia[xx][yy].x,jj=xia[xx][yy].y;
xx=ii,yy=jj;
} //print();
}
print();
}
return ;
}
/*
// //
*/
【链表】【模拟】Codeforces 706E Working routine的更多相关文章
- CodeForces 706E Working routine
十字链表. 开一个十字链表,矩阵中每一格作为一个节点,记录五个量: $s[i].L$:$i$节点左边的节点编号 $s[i].R$:$i$节点右边的节点编号 $s[i].U$:$i$节点上面的节点编号 ...
- Working routine CodeForces - 706E (链表)
大意: 给定矩阵, q个操作, 每次选两个子矩阵交换, 最后输出交换后的矩阵 双向十字链表模拟就行了 const int N = 1500; int n, m, q; struct _ { int v ...
- Codeforces 1131 F. Asya And Kittens-双向链表(模拟或者STL list)+并查集(或者STL list的splice()函数)-对不起,我太菜了。。。 (Codeforces Round #541 (Div. 2))
F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [cf div 2 706E] Working routine
[cf div 2 706E] Working routine Vasiliy finally got to work, where there is a huge amount of tasks w ...
- 【洛谷】NOIP提高组模拟赛Day2【动态开节点/树状数组】【双头链表模拟】
U41571 Agent2 题目背景 炎炎夏日还没有过去,Agent们没有一个想出去外面搞事情的.每当ENLIGHTENED总部组织活动时,人人都说有空,结果到了活动日,却一个接着一个咕咕咕了.只有不 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 模拟 Codeforces Round #203 (Div. 2) C. Bombs
题目地址:http://codeforces.com/problemset/problem/350/C /* 题意:机器人上下左右走路,把其他的机器人都干掉要几步,好吧我其实没读懂题目, 看着样例猜出 ...
- 模拟 Codeforces Round #249 (Div. 2) C. Cardiogram
题目地址:http://codeforces.com/contest/435/problem/C /* 题意:给一组公式,一组数据,计算得到一系列的坐标点,画出折线图:) 模拟题:蛮恶心的,不过也简单 ...
- [POJ1193][NOI1999]内存分配(链表+模拟)
题意 时 刻 T 内存占用情况 进程事件 0 1 2 3 4 5 6 7 8 9 进程A申请空间(M=3, P=10)<成功> 1 A 2 A B 进程B申请空间(M=4, P=3)< ...
随机推荐
- 零基础学习云计算及大数据DBA集群架构师【预科2015年12月14日周一】
1.第一天比较轻松,上午填表格,录指纹,拍照片,做自我介绍. 2.下午老师简单介绍了一下PC\交换机\路由器\塔式服务器\机架式服务器(1U\2U)\刀片服务器\磁带机 3.班主任陈老师朱老师,预科秦 ...
- Java-Android 之短信发送
file:///F:/workspace3/Android_ver2.5/src/cn/szy/com/MainActivity.java package cn.szy.com; import jav ...
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
--http://www.5idev.com/p-php_preg_match.shtml 正则表达式在 PHP 中的应用 在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正则表达式匹配相应的 ...
- IOC之Unity
项目最近用到,查找到比较好的资料,记录哈: Unity 3(一):简介与示例: Unity 3(二):Unity在AOP方面的应用
- basicAnimation移动图形
目的:采用CABasicAnimation 点击屏幕上的点来是实现图像的位置移动 并且位置能够不反弹 难点:1 通过动画的KeyPath找到layer的属性 2 通过NSValue将点包装成对象 ...
- idea配置tomcat.md
[toc] 1.打开Edit Configurations,可以通过万能搜索快速进入!!! 2.添加服务器,在左上角找到Tomcat并添加 3.配置发布路径,Server标签页中填写完名称和路径,在D ...
- 【POJ2266】【树状数组+离散化】Ultra-QuickSort
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- 《vi中的替换艺术》-linux命令五分钟系列之十一
vi方面的内容不知道分类到哪里好,就放到<Linux命令五分钟系列>里吧! 今天编程,关于栈的一个小例子,其间我需要把”S.”替换为”S->”(替换不包括双引号). 其实这个不难,不 ...
- 固定DIV样式
<!doctype html> <html> <head> <meta charset="UTF-8"> < ...
- An erroroccurred while filtering resources
maven报错: maven An error occurred while filtering resources Maven -> Update Project... resolved th ...