算下来真的欠了很多天的没写了,感觉最近状态不好,比较浮躁。
考试
T1 mosaic 100 哈哈哈,竟然不会四舍五入= =||
T2 jury 10 题目真的太绕了搞得我都不想做了
T3 forlove 50 后面5个点超时了,因为我的逆序数写的是O(n^2)…….
讲题
T1 就是模拟题意算一下输出。。
T2 贪心,排几次序,分别按照甲乙两人的要求排序。
T3 这个题就是判断N数码是否有解。如何判断呢?
直接给出结论吧,反正我也写不出好的证明,太绕了,网上有解释自行搜索。
有以下two定义:
①奇偶性——是指讲n*n的矩形数阵以一列数的形式排好后除0的逆序数之和的奇偶性。
e.g.
1 2 3
4 5 6
7 8 0
转化成:1 2 3 4 5 6 7 8 0 这里的逆序数之和为0,因为不包括0的在内。
②距离——是指给出矩阵中0所在行到目标矩阵中0所在行的差值。【注意这里是行差值,与列无关】
当n为奇数时,只要当前状态与目标状态奇偶性一致即可;
当n为偶数时,若【距离】是奇数,【奇偶性】为偶即yes;若【距离】是偶数,【奇偶性】为奇即yes;其余则no。
回家查了一下逆序数,好像是用的归并的思想可以做。
下面是我考试时的代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn (100+5)
int k, n, dis, pos, sum;
int a[maxn];
template <typename T>
T read(){
T N(0), F(1);
char C = getchar();
for(; !isdigit(C); C = getchar()) if(C == '-') F = -1;
for(; isdigit(C); C = getchar()) N = N*10 + C-48;
return N * F;
}
int main(){
freopen("forlove.in", "r", stdin);
freopen("forlove.out","w",stdout);
k = read<int>();
while(k--){
sum = 0; pos = 0; dis = 0;
n = read<int>();
for(int i = 1; i <= n*n; i++){
a[i] = read<int>();
if(!a[i]) pos = i;
}
for(int i = 2; i <= n*n; i++){
if(!a[i]) continue;
for(int j = 1; j < i; j++){
if(a[j] > a[i]) sum++;
}
}
if(n&1){
if(sum&1) printf("You are destined to be single.\n");
else printf("You still have a chance.\n");
}
else{
if(pos % n == 0) dis = abs(pos/n - n);
else dis = abs(pos/n+1-n);
if(dis&1){
if(sum&1) printf("You still have a chance.\n");
else printf("You are destined to be single.\n");
}
else{
if(sum&1) printf("You are destined to be single.\n");
else printf("You still have a chance.\n");
}
}
}
}
明天/后天会写一次逆序数。感觉归并的思想还很有用。