【HDU 6048 Puzzle】 逆序对 & 思维

来源:互联网 发布:斗牛牌型算法 编辑:程序博客网 时间:2024/06/06 11:11

Puzzle

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 446 Accepted Submission(s): 242

Problem Description
A Jigsaw puzzle contains N*M-1 pieces of jigsaws in a N rows*M columns rectangular board.Each jigsaw has a distinct number from 1 to N*M-1.Li is a naughty boy,he wants to arrange the board in his unique way.At the beginning,he picks all N*M-1 jigsaws out and put them on the table and then he will put them back to the board respecting the following steps:
1.Sorting all the remaining jigsaws on the table in ascending order.
2.Picking out the 1st ,the P+1 th ,the 2*P+1 th,……the n*P+1 th jigsaws and put them back to the blank area in the board one by one from the top row to the bottom row,from the left column to the right column.
3.if there are jigsaws remained on the table,back to step 1.
After he arranging the board,it’s obvious that there’s only one blank area located at the bottom-right corner.
Your task is to make the numbers on jigsaws sorted with every row and every column in ascending order(From left to right,top to bottom),and the blank area should be located at the bottom-right corner in the end.Each step you can move the blank area’s neighboring jigsaws(which share a common side with the blank area) towards the blank area.It’s really a difficult question,so you need to write a program to judge whether it is possible to complete the task.

这里写图片描述

这里写图片描述

Input
The first line contains an integer T(T<=100),which represents the number of test cases.
Following T lines,each line contains three integers N,M,P(2<=N,M<=1000;1<=P<=N*M-2).

Output
For each test case,print “YES” in a separate line if it is possible to complete the task ,otherwise please print “NO”.

Sample Input
3
3 2 3
3 2 4
999 999 1

Sample Output
YES
NO
YES

Source
2017 Multi-University Training Contest - Team 2

题意 : 有 n * m - 1 个数,每次选择第 1,p + 1,p * 2 + 1…..
的顺序选择数,按从上到下,从左到右的顺序填入n * m 的格子,空格子可以和相邻的数字交换位置,问最后能否在格子中形成 1~ n * m - 1的数按从左到右,从上到下的顺序

思路 : 考虑逆序对,只有当逆序对为偶数的时候可以做到,对于每次选取的每个数,都有 p * (i - 1) < p - 1 个数 < p * i,而这p - 1个数都可以和 >= p * i 的数可以产生的逆序对数,分别是本次可以选择的数 o - 1,o - 2…..,然后求和下为 o * (o - 1) / 2 * (p - 1)~

AC代码:

#include<cstdio>int main(){    int T;    scanf("%d",&T);    while(T--){        int n,m,p;        scanf("%d %d %d",&n,&m,&p);        int ans = 0,o = n * m - 1;        while(o > p){            int w = (o / p) + (o % p != 0);            ans += w * (w - 1) / 2 * (p - 1);            o -= w;        }        if(ans & 1) puts("NO");        else puts("YES");    }    return 0;}
原创粉丝点击