Captain Marmot(Codeforces Round #271 div2) C

Captain Marmot

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d
& %I64u

Submit Status

Description

Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.

Initially, each mole i (1?≤?i?≤?4n) is placed at some position (xi,?yi) in
the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it‘s possible.

Each mole i has a home placed at the position (ai,?bi).
Moving this mole one time means rotating his position point (xi,?yi)90 degrees
counter-clockwise around it‘s home point (ai,?bi).

A regiment is compact only if the position points of the 4 moles form a square with non-zero area.

Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it‘s possible.

Input

The first line contains one integer n (1?≤?n?≤?100), the number of regiments.

The next 4n lines contain 4 integers xiyiaibi (?-?104?≤?xi,?yi,?ai,?bi?≤?104).

Output

Print n lines to the standard output. If the regiment i can be made compact, the i-th
line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes).

Sample Input

Input

4
1 1 0 0
-1 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-2 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-1 1 0 0
-1 1 0 0
-1 1 0 0
2 2 0 1
-1 0 0 -2
3 0 0 -2
-1 1 -2 0

Output

1
-1
3
3

Hint

In the first regiment we can move once the second or the third mole.

We can‘t make the second regiment compact.

In the third regiment, from the last 3 moles we can move once one and twice another one.

In the fourth regiment, we can move twice the first mole and once the third mole.

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
struct node {
    int x,y;
} rec[10][10],cen[10];
LL dis(struct node a,struct node b)
{
    LL xx,yy;
    LL res;
    xx=(a.x-b.x)*(a.x-b.x);
    yy=(a.y-b.y)*(a.y-b.y);
    res=xx+yy;
    return res;
}
LL bian[6];
//LL xie[2];
void judge()
{
    int i,j,k,l;
    int ans=inf;
    for(i=0; i<4; i++)
        for(j=0; j<4; j++)
            for(k=0; k<4; k++)
                for(l=0; l<4; l++) {
                    bian[0]=dis(rec[i][0],rec[j][1]);
                    bian[1]=dis(rec[j][1],rec[k][2]);
                    bian[2]=dis(rec[k][2],rec[l][3]);
                    bian[3]=dis(rec[l][3],rec[i][0]);
                    bian[4]=dis(rec[i][0],rec[k][2]);
                    bian[5]=dis(rec[j][1],rec[l][3]);
                    //for(int ii=0;ii<6;ii++)
                    //printf("bian[%d]==%d",ii,bian[ii]);
                    sort(bian,bian+6);
                    if(bian[0]==0||bian[1]==0||bian[2]==0||bian[3]==0||bian[4]==0||bian[5]==0)
                        continue;
                    //else if(bian[0]==bian[1]&&bian[1]==bian[2]&&bian[2]==bian[3]&&bian[3]==bian[0]&&xie[0]==xie[1]&&bian[0]*2==xie[0])
                    // ans=min(ans,i+j+k+l);
                    else if(bian[0]==bian[1]&&bian[1]==bian[2]&&bian[2]==bian[3]&&bian[3]==bian[0]&&bian[4]==bian[5]&&bian[0]*2==bian[4])
                        ans=min(ans,i+j+k+l);
                }
    if(ans!=inf)
        printf("%d\n",ans);
    else
        printf("-1\n");

}
int main()
{
    int T,i,j;
    scanf("%d",&T);
    while(T--) {
        for(i=0; i<4; i++) {
            scanf("%d %d",&rec[0][i].x,&rec[0][i].y);
            scanf("%d %d",&cen[i].x,&cen[i].y);
            rec[1][i].x=cen[i].x-(rec[0][i].y-cen[i].y);
            rec[1][i].y=cen[i].y+(rec[0][i].x-cen[i].x);
            rec[2][i].x=cen[i].x-(rec[0][i].x-cen[i].x);
            rec[2][i].y=cen[i].y-(rec[0][i].y-cen[i].y);
            rec[3][i].x=cen[i].x+(rec[0][i].y-cen[i].y);
            rec[3][i].y=cen[i].y-(rec[0][i].x-cen[i].x);
        }
        judge();
    }
    return 0;
}
时间: 2024-10-29 10:46:34

Captain Marmot(Codeforces Round #271 div2) C的相关文章

codeforces round #257 div2 C、D

本来应该认真做这场的,思路都是正确的. C题,是先该横切完或竖切完,无法满足刀数要求,再考虑横切+竖切(竖切+横切), 因为横切+竖切(或竖切+横切)会对切割的东西产生交叉份数,从而最小的部分不会尽可能的大. 代码如下,虽然比较长.比较乱,但完全可以压缩到几行,因为几乎是4小块重复的代码,自己也懒得压缩 注意一点,比如要判断最小块的时候,比如9行要分成2份,最小的剩下那份不是9取模2,而应该是4 m/(k+1)<=m-m/(k+1)*k          #include<bits/stdc+

codeforces Round #250 (div2)

a题,就不说了吧 b题,直接从大到小排序1-limit的所有数的lowbit,再从大到小贪心组成sum就行了 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #define N 200000 6 using namespace std; 7 int pos[N],a[N],s[N],f[N],la[N],b[N],i,j,k,ans,n,p

Codeforces Round#320 Div2 解题报告

Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Finding Team Member codeforces 579C A Problem about Polyline codeforces 579D "Or" Game codeforces 579E Weakness and Poorness codeforces 579F LCS Aga

Codeforces Round #254(div2)A

很有趣的题.想到了就非常简单,想不到就麻烦了. 其实就是一种逆向思维:最后结果肯定是这样子: WBWBWBWB... BWBWBWBW... WBWBWBWB... ... 里面有“-”的地方改成“-”就行了. 但是我开始是正着想的,想每个点怎么处理,这还要看它周围点的状态,越想越麻烦... 这题中体现的正难则反的逆向思维很值得学习. #include<iostream> #include<cstdio> #include<cstdlib> #include<cs

Codeforces Round #254(div2)B

就是看无向图有几个连通块,答案就是2n-num. 范围很小,就用矩阵来存图减少代码量. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm> #inc

Codeforces Round #260(div2)C(递推)

有明显的递推关系: f[i]表示i为数列中最大值时所求结果.num[i]表示数i在数列中出现了几次. 对于数i,要么删i,要么删i-1,只有这两种情况,且子问题还是一样的思路.那么很显然递推一下就行了:f[i]=max(f[i-1],f[i-2]+i*num[i]); 这里技巧在于:为了防止麻烦,干脆就所有数的出现次数都记录一下,然后直接从2推到100000(类似于下标排序),就不用排序了,也不用模拟删除操作了.这一技巧貌似简单,但实际上临场想出来也需要点水平. #include<iostrea

Codeforces Round #289 Div2 E

Problem 给一串长度为N的字符串,对于每个字符,若字符为元音,则权值为1,否则为0.一个子串的权值定义为该串所有字符权值之和除以字符个数,一个母串的权值定义为所有子串的权值之和.求母串的权值. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N: [1, 5*10^5] 字符集: 'A'-'Z' 元音: I E A O U Y Solution 考虑每个元音字符对母串的贡献,可以找出规律. More 举"ABCDOEFGHKMN"

Codeforces Round #403 div2 C. Andryusha and Colored Balloons

题目链接:Codeforces Round #403 div2 C. Andryusha and Colored Balloons 题意: 给你一棵n个节点的树,然后让你染色,规定相连的三个 节点不能同色,问需要的最少颜色,并输出其中一种方案. 题解: 因为只有相邻3个节点不同色. 所以直接DFS,每个节点都从1开始. 然后ans[v]!=ans[u]!=ans[fa]就行. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i&

CodeForces Round#229 DIV2 C 递推DP

对这道题目也只好说呵呵了,没注意k的范围最大才10,所以昨晚纠结了很久,没什么好的方法来处理,后来无奈想去翻翻题解,发现人家开头就来了句,因为k的范围比较小 所以.........我只好暂停马上回头看看题目,是的,k比较小所以完全可以先在询问前预处理DP一遍, DP就比较清晰了,dp[i][j]  (i>=0 && i<k,,,,j>=i && j <=n)代表意义呢 以i为开头的  区间[1,j]注意 是 1~j的 所需要的操作数,题目问的是最小操