HDU——Cover——————【技巧】

Cover

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1027    Accepted Submission(s): 351
Special Judge

Problem Description

You have an n∗n matrix.Every grid has a color.Now there are two types of operating:
L x y: for(int i=1;i<=n;i++)color[i][x]=y;
H x y:for(int i=1;i<=n;i++)color[x][i]=y;
Now give you the initial matrix and the goal matrix.There are m operatings.Put in order to arrange operatings,so that the initial matrix will be the goal matrix after doing these operatings

It‘s guaranteed that there exists solution.

Input

There are multiple test cases,first line has an integer T
For each case:
First line has two integer n,m
Then n lines,every line has n integers,describe the initial matrix
Then n lines,every line has n integers,describe the goal matrix
Then m lines,every line describe an operating

1≤color[i][j]≤n
T=5
1≤n≤100
1≤m≤500

Output

For each case,print a line include m integers.The i-th integer x show that the rank of x-th operating is i

Sample Input

1

3 5

2 2 1

2 3 3

2 1 3

3 3 3

3 3 3

3 3 3

H 2 3

L 2 2

H 3 3

H 1 3

L 2 3

Sample Output

5 2 4 3 1

题目大意:给你一个n*n的矩阵,给你初始矩阵和目标矩阵,然后有m个操作。H x z表示将第x行覆盖为z,L x z表示将第x列覆盖为z,保证是有解。问你这m个操作怎么排,可以让初始矩阵变为目标矩阵。

解题思路:遍历m个操作,如果是行操作,就看该行是否都是所要染的颜色或着是0颜色,如果这一行跟要染的颜色一样,那么就存起来操作,同时把该行全部变为0,。由于不是一次下来就能得到结果,所以用一个变量记录已经有多少个操作已经在结果中,最后逆序输出即为答案。

#include<bits/stdc++.h>
using namespace std;
struct Oper{
    int r_,x,col;
}opers[550];
int Map[125][125],ans[550],vis[550];
int main(){
    int t,a,n,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&a);
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&Map[i][j]);
            }
        }
        int a,b;
        char str[20];
        for(int i=1;i<=m;i++){
            scanf("%s%d%d",str,&a,&b);
            if(str[0]==‘H‘){
                opers[i].r_=1;
                opers[i].x=a;
                opers[i].col=b;
            }else{
                opers[i].r_=0;
                opers[i].x=a;
                opers[i].col=b;
            }
        }
        memset(vis,0,sizeof(vis));
        int cnt=0;
        while(cnt<m){
            for(int i=1;i<=m;i++){
                if(!vis[i]){
                    if(opers[i].r_==1){
                        int r=opers[i].x,aim=opers[i].col;
                        int j;
                        for(j=1;j<=n;j++){
                            if(Map[r][j]!=aim&&Map[r][j]!=0){
                                break;
                            }
                        }
                        if(j==n+1){
                            for(j = 1;j<=n;j++){
                                Map[r][j]=0;
                            }
                            ans[cnt]=i;
                            cnt++;
                            vis[i]=1;
                        }
                    }else{
                        int c=opers[i].x,aim=opers[i].col;
                        int j;
                        for(j=1;j<=n;j++){
                            if(Map[j][c]!=aim&&Map[j][c]!=0){
                                break;
                            }
                        }
                        if(j==n+1){
                            for(j = 1;j<=n;j++){
                                Map[j][c]=0;
                            }
                            ans[cnt]=i;
                            cnt++;
                            vis[i]=1;
                        }
                    }
                }
            }
        }
        printf("%d",ans[cnt-1]);
        for(int i=cnt-2;i>=0;i--){
            printf(" %d",ans[i]);
        }printf("\n");
    }
    return 0;
}

  

时间: 2024-08-07 23:42:31

HDU——Cover——————【技巧】的相关文章

hdu 5265 技巧题 O(nlogn)求n个数中两数相加取模的最大值

pog loves szh II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2106    Accepted Submission(s): 606 Problem Description Pog and Szh are playing games.There is a sequence with n numbers, Pog wi

HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便,我们将小时换成分钟,那么一天24小时,总共有1440分钟.顾我就可以把一天里的任意HH:MM时间换成分钟.就这样一天的时间就变成[0,1440]区间了. 因为所给的活动最多是5*10^5,如果把活动的时间在线段[0,1440]都修改,那么时间的复杂度最坏是O(5*10^5*1440). (1)方法一

HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/details/52577212 [分析]一开始想简单了,对于a^x mod p这种形式的直接用欧拉定理的数论定理降幂了 结果可想而知,肯定错,因为题目并没有保证gcd(x,s+1)=1,而欧拉定理的数论定理是明确规定的 所以得另谋出路 那么网上提供了一种指数循环节降幂的方法 具体证明可以自行从网上找一找 有

HDU 2186 悼念512汶川大地震遇难同胞——一定要记住我爱你(取余技巧)

悼念512汶川大地震遇难同胞--一定要记住我爱你 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9582    Accepted Submission(s): 5826 Problem Description 当抢救人员发现她的时候,她已经死了,是被垮塌下来的房子压死的,透过那一堆废墟的的间隙可以看到她死亡的姿势,双膝跪着,整个上身向

HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧

前言 在编程过程中总结归纳出来的一种编程经验,从而形成的设计思想称为设计模式. 设计模式有23种.它适用于所有的编程语言. 常用的有创新型的设计模式:简单工厂.抽象工厂和单例模式:行为型的设计模式:模板设计模式.观察者模式和命令模式:结构性的设计模式:适配器设计模式.代理模式(静态和动态两种,典型的有在spring的AOP编程中使用)和装饰器设计模式. 正文 单例模式(singleton) 保证一个类在内存中只能创建一个实例. 1.实现步骤: 1)将构造器私有化,即使用private修饰构造器

hdu 4864 Task (贪心 技巧)

题目链接 一道很有技巧的贪心题目. 题意:有n个机器,m个任务.每个机器至多能完成一个任务.对于每个机器,有一个最大运行时间xi和等级yi, 对于每个任务,也有一个运行时间xj和等级yj.只有当xi>=xj且yi>=yj的时候,机器i才能完成任务j,并获得 500*xj+2*yj金钱.问最多能完成几个任务,当出现多种情况时,输出获得金钱最多的情况. 分析:机器和任务都按照先拍x从大到小,再拍y从大到小的顺序.然后遍历任务,注意f[]数组的作用,f[]数组标 记大于当前任务时间的机器的等级个数,

HDU -2674 N!Again(小技巧)

这道题有个小技巧,就是既然是n!,那么对2009求余,只要大于2009!,那么一定是0,在仔细想想会发现,根本到不了2009,只要到2009的最大质因数就行了,为什么呢?因为最大质因数是最大的一个不能被2009整除的,2009的最大质因数是41,也就是只要大于41的阶乘的都可以整除2009,因为41的阶乘可以整除,42! = 42 * 41!,所以大于41的全部为0,那么这个题就简单了 代码如下: 1 #include<iostream> 2 #include <cstdio> 3

HDU - 5665 Lucky (技巧)

HDU - 5665 Lucky Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Chaos August likes to study the lucky numbers. For a set of numbers S,we set the minimum non-negative integer,which can't be gotten

HDU 5386 Cover(模拟)

Cover Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 966    Accepted Submission(s): 320 Special Judge Problem Description You have an n?n matrix.Every grid has a color.Now there are two types