C. Maximize the Intersections - 构造 -思维

Problem - C - Codeforces

C. Maximize the Intersections

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

On a circle lie 2n2n distinct points, with the following property: however you choose 33 chords that connect 33 disjoint pairs of points, no point strictly inside the circle belongs to all 33 chords. The points are numbered 1,2,…,2n1,2,…,2n in clockwise order.

Initially, kk chords connect kk pairs of points, in such a way that all the 2k2k endpoints of these chords are distinct.

You want to draw n−kn−k additional chords that connect the remaining 2(n−k)2(n−k) points (each point must be an endpoint of exactly one chord).

In the end, let xx be the total number of intersections among all nn chords. Compute the maximum value that xx can attain if you choose the n−kn−k chords optimally.

Note that the exact position of the 2n2n points is not relevant, as long as the property stated in the first paragraph holds.

Input

The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then tt test cases follow.

The first line of each test case contains two integers nn and kk (1≤n≤1001≤n≤100, 0≤k≤n0≤k≤n) — half the number of points and the number of chords initially drawn.

Then kk lines follow. The ii-th of them contains two integers xixi and yiyi (1≤xi,yi≤2n1≤xi,yi≤2n, xi≠yixi≠yi) — the endpoints of the ii-th chord. It is guaranteed that the 2k2k numbers x1,y1,x2,y2,…,xk,ykx1,y1,x2,y2,…,xk,yk are all distinct.

Output

For each test case, output the maximum number of intersections that can be obtained by drawing n−kn−k additional chords.

Example

input

Copy

4
4 2
8 2
1 5
1 1
2 1
2 0
10 6
14 6
2 20
9 10
13 18
15 12
11 7

output

Copy

4
0
1
14

Note

In the first test case, there are three ways to draw the 22 additional chords, shown below (black chords are the ones initially drawn, while red chords are the new ones):

We see that the third way gives the maximum number of intersections, namely 44.

In the second test case, there are no more chords to draw. Of course, with only one chord present there are no intersections.

In the third test case, we can make at most one intersection by drawing chords 1−31−3 and 2−42−4, as shown below:

---------------------------------------------------------------------------------------------------------------------------------

毫无规律可循的题目,给出的原始弦千变万化,贪心,排序

根本行不通。

猜想是一种固定格式的构造,不随原始弦的多样性而变化。首先无原始弦时,自然是 (n-1)*n/2个交点,构造方法是 每一个节点i  和i+n进行连接,这样获得的交点是最多的。

猜测构造方法为 ,提取出尚未匹配的节点,将其抽象为另一个圆,由于节点之间距离并不等距,可以任意安排,所以一定会有上文规律个交点。再进行判断即可。

# include<bits/stdc++.h>

using namespace std;
typedef long long int ll;

int temp[1000];
int x[1000],y[1000],book[1000];
int main ()
{

   int t;
   cin>>t;

   while(t--)
   {
       int n,k;
       cin>>n>>k;

       for(int i=1;i<=2*n;i++)
       {
           book[i]=0;
       }
       for(int i=1;i<=k;i++)
       {
           cin>>x[i]>>y[i];
           book[x[i]]=1;
           book[y[i]]=1;
       }
       int len=0;
       for(int i=1;i<=2*n;i++)
       {
           if(book[i]==0)
           {
              len++;
              temp[len]=i;

             // cout<<i<<" ";
           }
       }


       for(int i=k+1;i<=n;i++)
       {
           x[i]=temp[i-k];
           y[i]=temp[i-k+(n-k)];


       }

       int ans=0;
       for(int i=1;i<=n;i++)
       {
           for(int j=i+1;j<=n;j++)
           {

               int x1=x[i],y1=y[i];
               int x2=x[j],y2=y[j];
               int maxx=max(x1,y1),minn=min(x1,y1);
               if(x2>minn&&x2<maxx&&(y2<minn||y2>maxx))
               {
                   ans++;
                   continue;
               }
               if(y2>minn&&y2<maxx&&(x2<minn||y2>maxx))
               {
                   ans++;
                   continue;
               }
               maxx=max(x2,y2),minn=min(x2,y2);
               if(x1>minn&&x1<maxx&&(y1<minn||y1>maxx))
               {
                   ans++;
                   continue;
               }

               if(y1>minn&&y1<maxx&&(x1<minn||y1>maxx))
               {
                   ans++;
                   continue;
               }
           }
       }

       cout<<ans<<endl;


   }

    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值