CodeChef SnakeDown2017 E解题报告

来源:互联网 发布:软件性能指标描述 编辑:程序博客网 时间:2024/06/08 10:11




The kingdom of the snakes is an NxN grid. Their most-valued possession is a huge collection of poison, which is stored in the centralKxK grid. It is guaranteed that bothN andK are odd. What we mean by 'central' is this: suppose in theNxN grid, (i, j) refers to the cell in the i-th row and j-th column and (1,1) refers to the top-left corner and (N,N) refers to the bottom-right corner. Then the poison is stored in theKxK square whose top-left corner is ( (N - K)/2 + 1, (N - K)/2 + 1 ).

But there are thieves who want to steal the poison. They cannot enter the NxN grid, but they can shoot arrows from outside. These arrows travel across a row (from left to right, or right to left), or across a column (top to bottom or bottom to top) in a straight line. If the arrow enters theKxK grid, some of the poison will stick to the arrow, and if it exits theNxN grid after that, the thieves will have successfully stolen some of the poison.

As the King of the snakes, you want to thwart these attempts. You know that the arrows will break and stop if they hit a snake's scaly skin, but won't hurt the snakes. There are some snakes already guarding the poison. Each snake occupies some consecutive cells in a straight line inside the NxN grid. That is, they are either part of a row, or part of a column. Note that there can be intersections between the snakes. A configuration of snakes is 'safe', if the thieves cannot steal poison. That is, no matter which row or column they shoot arrows from, either the arrow should hit a snake and stop (this can happen even after it has entered and exited theKxK grid), or it shouldn't ever enter theKxK grid.

The King has other duties for the snakes, and hence wants to remove as many snakes as possible from this configuration, such that the remaining configuration is still 'safe'. Help him find the minimum number of snakes he needs to leave behind to protect the poison.

Input

  • The first line contains a single integer, T, the number of testcases.
  • The first line of each testcase contains three integers: N, K andM, whereN is the size of the entire square grid,K is the size of the square containing the poison, andM is the number of initial snakes.
  • M lines follow, the i-th of which contains four integers: HXi, HYi, TXi, TYi. (HXi, HYi) is the cell which contains the head of the i-th snake. (TXi, TYi) is the cell which contains the tail of the i-th snake. It is guaranteed that both these cells will either lie in the same row, or same column. And hence the cells in between them, including these two cells, represents the i-th snake.

Output

  • For each testcase, output a single integer in a new line: The minimum number of the snakes that the king can keep and still protect the poison. If it is not possible to protect the poison even with all the snakes, output -1.

Constraints

  • 1 ≤ T ≤ 4
  • 3 ≤ N ≤ 109
  • 1 ≤ KN-2
  • Both N and K will be odd integers
  • 1 ≤ M ≤ 105
  • 1 ≤ HXi, HYi, TXi, TYiN
  • It is guaranteed that at least one of (HXi = TXi), and (HYi = TYi) will be true for all i
  • None of the cells in the KxK grid will be occupied by any snake

Example

Input:27 3 71 1 6 11 2 3 25 2 5 22 4 2 66 2 6 45 6 5 77 1 7 47 3 71 1 6 11 2 3 25 2 5 22 6 2 66 2 6 45 6 5 77 1 7 4Output:3-1


简单的说,就是两次计算区间最少线段覆盖。

用dp[i]来记录用了i个线段能够覆盖的区间最远点,然后要先排序线段左端点,每次由用那些左端点小于dp[i]的线段的右端点值来更新dp[i+1]就没了。

尤其要注意的是线段的读入预处理操作,比如有些线段全在四个角落里,那就直接滤掉他。

再比如读入一个线段是横着的,他不一定是用来覆盖行的,也可以是列的,比如说

7 3 3

2 3 2 5

3 1 3 2

4 2 5 2

这组数据也是OK的。被这个东西卡了好久。

#include<iostream>#include<stdio.h>#include<vector>#include<map>#include<algorithm> #include<cstring>using namespace std;#define min(a,b) (a)<(b)?(a):(b) #define maxx(a,b) (a)>(b)?(a):(b)int m,n,k,t,x1,x2,y1,y2;struct node{int a1,a2,b;}line[100005],row[100005];bool cmp(node n1,node n2){return n1.a1<n2.a1;}int dp[100005];int main(){cin>>t;for (int o=0;o<t;o++){cin>>n>>k>>m;int min=(n-k)/2+1;int max = (n+k)/2;int linetot=0,rowtot=0;for (int i=0;i<m;i++){scanf("%d%d%d%d",&x1,&y1,&x2,&y2);if (x1==x2){node temp;temp.b=x2;temp.a1=min(y1,y2);temp.a2=maxx(y1,y2);if (temp.a2<min||temp.a1>max){if (temp.b>=min&&temp.b<=max){temp.a1=temp.a2=temp.b;row[rowtot]=temp;rowtot++;}}else{line[linetot]=temp;linetot++;}}if (y1==y2){node temp;temp.b=y1;temp.a1=min(x1,x2);temp.a2=maxx(x1,x2);if (temp.a2<min||temp.a1>max){ if (temp.b>=min&&temp.b<=max){ temp.a1=temp.a2=temp.b; line[linetot]=temp; linetot++; }}else{row[rowtot]=temp;rowtot++;} }}sort(line+0,line+linetot,cmp);sort(row+0,row+rowtot,cmp);//for (int i=0;i<linetot;i++){//cout<<"line["<<i<<"]  a1:"<<line[i].a1<<"  a2:"<<line[i].a2<<endl;//}//for (int i=0;i<rowtot;i++){//cout<<"row["<<i<<"]   a1:"<<row[i].a1<<"   a2:"<<row[i].a2<<endl;//}//dp.clear();memset(dp,0,sizeof(dp));dp[0]=min-1;int index=0; int lineans=-1;bool flag=false;//cout<<min<<" "<<max<<endl; for (int i=0;i<=m;i++){dp[i+1]=dp[i];while (true){if (index<linetot&&line[index].a1<=dp[i]+1){if (line[index].a2>=max){//cout<<i+1<<endl;lineans=i+1;flag=true;break;}dp[i+1]=maxx(dp[i+1],line[index].a2);index++;}else{break;}}if (flag){break;}}if (!flag){cout<<-1<<endl;continue;}flag=false;//dp.clear();memset(dp,0,sizeof(dp));index=0;int rowans=-1;dp[0]=min-1;for (int i=0;i<=m;i++){dp[i+1]=dp[i];while (true){if(index<rowtot&&row[index].a1<=dp[i]+1){if (row[index].a2>=max){rowans=i+1;flag=true;break;}dp[i+1]=maxx(dp[i+1],row[index].a2);index++;}else{break;}}if (flag){break;}}if (!flag){cout<<-1<<endl;continue;}cout<<lineans+rowans<<endl;//cout<<rowans+lineans<<endl;}} 





原创粉丝点击