USACO 1.3 Wormholes

摘要:
一些虫洞可能会导致“无限循环”。例如,1和2位于同一条线上,2位于1的右侧,2和1链接在一起。他们从1开始向右走。当它们达到2时,它们将被传递到1,然后传递到2,然后传递给1。它们永远不会出来。现在,给定n个虫洞的坐标,询问有多少个虫洞匹配方法可以导致至少一个“无限循环”。(在示例中,12个匹配和34个匹配可以形成两个无限循环,但对答案的贡献是一个。匹配后,检查一次,看看是否有任何点的“无限循环”。如果有,ans++。
Wormholes

Farmer John's hobby of conducting high-energy physics experiments on weekends has backfired, causing N wormholes (2 <= N <= 12, N even) to materialize on his farm, each located at a distinct point on the 2D map of his farm (the x,y coordinates are both integers).

According to his calculations, Farmer John knows that his wormholes will form N/2 connected pairs. For example, if wormholes A and B are connected as a pair, then any object entering wormhole A will exit wormhole B moving in the same direction, and any object entering wormhole B will similarly exit from wormhole A moving in the same direction. This can have rather unpleasant consequences.

For example, suppose there are two paired wormholes A at (1,1) and B at (3,1), and that Bessie the cow starts from position (2,1) moving in the +x direction. Bessie will enter wormhole B [at (3,1)], exit from A [at (1,1)], then enter B again, and so on, getting trapped in an infinite cycle!

   | . . . .
   | A > B .      Bessie will travel to B then
   + . . . .      A then across to B again

Farmer John knows the exact location of each wormhole on his farm. He knows that Bessie the cow always walks in the +x direction, although he does not remember where Bessie is currently located.

Please help Farmer John count the number of distinct pairings of the wormholes such that Bessie could possibly get trapped in an infinite cycle if she starts from an unlucky position. FJ doesn't know which wormhole pairs with any other wormhole, so find all the possibilities.

PROGRAM NAME: wormhole

INPUT FORMAT:

Line 1:The number of wormholes, N.
Lines 2..1+N:Each line contains two space-separated integers describing the (x,y) coordinates of a single wormhole. Each coordinate is in the range 0..1,000,000,000.

SAMPLE INPUT (file wormhole.in):

4
0 0
1 0
1 1
0 1

INPUT DETAILS:

There are 4 wormholes, forming the corners of a square.

OUTPUT FORMAT:

Line 1:The number of distinct pairings of wormholes such that Bessie could conceivably get stuck in a cycle walking from some starting point in the +x direction.

SAMPLE OUTPUT (file wormhole.out):

2

OUTPUT DETAILS:

If we number the wormholes 1..4 as we read them from the input, then if wormhole 1 pairs with wormhole 2 and wormhole 3 pairs with wormhole 4, Bessie can get stuck if she starts anywhere between (0,0) and (1,0) or between (0,1) and (1,1).

   | . . . .
   4 3 . . .      Bessie will travel to B then
   1-2-.-.-.      A then across to B again

Similarly, with the same starting points, Bessie can get stuck in a cycle if the pairings are 1-3 and 2-4 (if Bessie enters WH#3 and comes out at WH#1, she then walks to WH#2 which transports here to WH#4 which directs her towards WH#3 again for a cycle).

Only the pairings 1-4 and 2-3 allow Bessie to walk in the +x direction from any point in the 2D plane with no danger of cycling.

题目大意:一个农场,上面有N个虫洞(N是偶数),虫洞两两链接,从一端进入会从另一端出来,出来的时候方向不改变。有的虫洞可能会造成“无限循环”,比如1和2在同一直线上,2在1右边,2和1链接,从1出发向右走,到达2后又会传送到1,又到达2,传送到1,永远走不出来(行走方向恒向右),现在给出n个虫洞的坐标,问有多少种虫洞匹配方式可以造成至少一个“无限循环”。

(在样例里面,12匹配,34匹配,可以形成两个无限循环,但是对答案的贡献为一。)

一开始就在寻找通式,想了半小时,有点思路(以为是个DP+数论的题)准备写代码,去确认数据范围才发现发现n居然只有12,那不是各种暴力么。。。。。。

思考前确认数据范围

思考前确认数据范围

思考前确认数据范围

首先确定了一种策略,保证每种匹配都会经历且只经历一次,就是先从12个点中挑出6个点(C 12,6),再对剩下的点全排列(A 6 6),与保留的点一一匹配,且必须保证每一对匹配,保留的点的编号小于挑选的点的编号(因为反过来是一样的,避免重复)。

匹配完之后check一次,看看有没有从任何一个点出发会陷入“无限循环”,如果有,ans++。看起开思路是无懈可击的(而且很简单),可是就是没过(跪在了第九组),先贴上来,一会儿对匹配策略修改下。

1 /*
2 ID:fffgrdcc1
3 LANG:C++
4 TASK:wormhole
5 */
6 //12个里面选6个,剩下6个全排列。
7 #include<cstdio>
8 #include<iostream>
9 #include<cstring>
10 #include<algorithm>
11 using namespacestd;
12 intn;
13 structstr
14 {
15     intx,y;
16 }e[13];
17 //bool vis[12]={0};
18 int matcha[6],matchb[6];
19 boolkong(str x,str y)
20 {
21     return (x.y<y.y||(x.y==y.y&&x.x<y.x));
22 }
23 int jiecheng[6]={1,2,6,24,120,720};
24 int afterr[13],next[13]={0},ans=0,tota=0,totb=0;
25 boolcheck()
26 {
27     //int tot=0;
28     int flag=0;
29     for(int i=0;i<n&&!flag;i++)
30 {
31         int tot=0;
32         int nown=i;
33         while(nown)
34 {
35             tot++;
36             nown=next[nown];
37             //if(nown==0)
38             nown=afterr[nown];
39             if(tot>n+1)
40 {
41                 flag=1;
42                 break;
43 }
44 }
45 }
46     returnflag;
47 }
48 voidwork()
49 {
50     sort(matchb,matchb+n/2);
51     //printf("%d %d
",tota,totb);
52     for(int i=0;i<jiecheng[n/2-1];i++)
53 {
54         int flag=0;
55         for(int j=0;    j<n/2;j++)
56 {
57             if(matcha[j]>matchb[j])
58 {
59                 flag=1;
60                 break;
61 }
62 }
63         if(flag)
64 {
65             next_permutation(matchb,matchb+n/2);
66             continue;
67 }
68         /*
69 for(int j=0;j<6;j++)
70 printf("%d ",matcha[j]);printf("
");
71 for(int j=0;j<6;j++)
72 printf("%d ",matchb[j]);printf("
");printf("
");
73         */
74         for(int j=0;j<n/2;j++)
75 {
76             next[matchb[j]]=matcha[j];
77             next[matcha[j]]=matchb[j];
78 }
79         if(check())ans++;
80         next_permutation(matchb,matchb+n/2);
81 }
82     return;
83 }
84 void dfs(intv)
85 {
86     if(tota==totb&&tota==n/2)
87 {
88 work();
89 }
90     if(tota<n/2)
91 {
92         matcha[tota++]=v;
93         dfs(v+1);
94         tota--;
95 }
96     if(totb<n/2)
97 {
98         matchb[totb++]=v;
99         //vis[v]=1;
100         dfs(v+1);
101         totb--;
102 }
103     return;
104 }
105 intmain()
106 {
107     //freopen("wormhole.in","r",stdin);
108     //freopen("wormhole.out","w",stdout);
109     scanf("%d",&n);
110     for(int i=1;i<=n;i++)
111 {
112         scanf("%d%d",&e[i].x,&e[i].y);
113 }
114     sort(e+1,e+n+1,kong);
115     for(int i=1;i<n;i++)
116 {
117         if(e[i].y==e[i+1].y)afterr[i]=i+1;
118 }
119     dfs(1);
120     printf("%d
",ans);
121     return 0;
122 }

附上卡我的数据

Here are the respective outputs:
        ----- our output ---------
        7350
        ---- your output ---------
        7005
        --------------------------

        ------ Data for Run 9 [length=243 bytes] ------
        12 
        572085931 667578536 
        964406504 667578536 
        656852339 870264627 
        110654368 823223484 
        513786208 528178006 
        620147001 528178006 
        227047539 667578536 
        656852339 528178006 
        945298921 528178006 
        945298921 870264627 
        840030425 870264627 
        828839382 528178006 
        ----------------------------

下面的思路是这样的,12个里面选1号,然后在剩下的里面随机选择一个和它组成一个匹配,剩下的里面选择编号最小的,再随机选一个和他匹配,重复该过程直到选完所有的点。复杂度低于上面这个?最大种数为:11*9*7*5*3*1=10^4;

1 /*
2 ID:fffgrdcc1
3 LANG:C++
4 TASK:wormhole
5 */
6 //12个里面选6个,剩下6个全排列。
7 #include<cstdio>
8 #include<iostream>
9 #include<cstring>
10 #include<algorithm>
11 using namespacestd;
12 intn;
13 structstr
14 {
15     intx,y;
16 }e[13];
17 int vis[13]={0};
18 //bool vis[12]={0};
19 int matcha[6],matchb[6];
20 boolkong(str x,str y)
21 {
22     return (x.y<y.y||(x.y==y.y&&x.x<y.x));
23 }
24 int jiecheng[6]={1,2,6,24,120,720};
25 int afterr[13],next[13]={0},ans=0,tota=0,totb=0;
26 boolcheck()
27 {
28     //int tot=0;
29     int flag=0;
30     for(int i=0;i<n&&!flag;i++)
31 {
32         int tot=0;
33         int nown=i;
34         while(nown)
35 {
36             tot++;
37             nown=next[nown];
38             //if(nown==0)
39             nown=afterr[nown];
40             if(tot>n+1)
41 {
42                 flag=1;
43                 break;
44 }
45 }
46 }
47     returnflag;
48 }
49 voidwork()
50 {
51     for(int j=0;j<n/2;j++)
52 {
53         next[matchb[j]]=matcha[j];
54         next[matcha[j]]=matchb[j];
55 }
56     if(check())ans++;
57     return;
58 }
59 void dfs(intv)
60 {
61     if(tota==totb&&tota==n/2)
62 {
63 work();
64 }
65     if(v==7)return;
66     int minn=100;
67     for(int i=1;i<=n;i++)
68 {
69         if(!vis[i])
70 {
71             minn=i;
72             matcha[tota++]=i;
73             vis[i]=1;
74             break;
75 }
76 }
77     for(int i=1;i<=n;i++)
78 {
79         if(!vis[i])
80 {
81             matchb[totb++]=i;
82             vis[i]=1;
83             dfs(v+1);
84             vis[i]=0;
85             totb--;
86 }
87 }
88     vis[minn]=0;
89     tota--;
90 }
91 intmain()
92 {
93     freopen("wormhole.in","r",stdin);
94     //freopen("wormhole.out","w",stdout);
95     scanf("%d",&n);
96     for(int i=1;i<=n;i++)
97 {
98         scanf("%d%d",&e[i].x,&e[i].y);
99 }
100     sort(e+1,e+n+1,kong);
101     for(int i=1;i<n;i++)
102 {
103         if(e[i].y==e[i+1].y)afterr[i]=i+1;
104 }
105     dfs(1);
106     printf("%d
",ans);
107     return 0;
108 }

惊了。。。还是7005,答案应该是7350的。。。。这让我不得不思考是不是我的check函数跪了。。。

总算过掉了。。。注意上面第30行,正确的写法应该从1到n

边界的重要性

,改完后第九组果然过了,开开心心的提交,结果第一组跪了。。。。注意第70行,应该把7改为n/2+1

提交前检查样例!

不要用特殊情况,随时保证自己的程序写的是普适算法

正确代码如下。。。

1 /*
2 ID:fffgrdcc1
3 LANG:C++
4 TASK:wormhole
5 */
6 //12个里面选6个,剩下6个全排列。
7 #include<cstdio>
8 #include<iostream>
9 #include<cstring>
10 #include<algorithm>
11 using namespacestd;
12 intn;
13 structstr
14 {
15     intx,y;
16 }e[13];
17 int vis[13]={0};
18 int matcha[6],matchb[6];
19 boolkong(str x,str y)
20 {
21     return (x.y<y.y||(x.y==y.y&&x.x<y.x));
22 }
23 int jiecheng[6]={1,2,6,24,120,720};
24 int afterr[13],next[13]={0},ans=0,tota=0,totb=0;
25 int partner[13];
26 boolcheck()
27 {
28     //int tot=0;
29     int flag=0;
30     for(int i=1;i<=n&&!flag;i++)
31 {
32         int tot=0;
33         int nown=i;
34         while(nown)
35 {
36             tot++;
37             nown=next[nown];
38             nown=afterr[nown];
39             if(tot>n+1)
40 {
41                 flag=1;
42                 break;
43 }
44 }
45 }
46     returnflag;
47 }
48 voidwork()
49 {
50     for(int j=0;j<n/2;j++)
51 {
52         next[matchb[j]]=matcha[j];
53         next[matcha[j]]=matchb[j];
54 }
55     if(check())ans++;
56     return;
57 }
58 void dfs(intv)
59 {
60     if(tota==totb&&tota==n/2)
61 {
62 work();
63 }
64     if(v==n/2+1)return;
65     int minn=100;
66     for(int i=1;i<=n;i++)
67 {
68         if(!vis[i])
69 {
70             minn=i;
71             matcha[tota++]=i;
72             vis[i]=1;
73             break;
74 }
75 }
76     for(int i=1;i<=n;i++)
77 {
78         if(!vis[i])
79 {
80             matchb[totb++]=i;
81             vis[i]=1;
82             dfs(v+1);
83             vis[i]=0;
84             totb--;
85 }
86 }
87     vis[minn]=0;
88     tota--;
89 }
90 intmain()
91 {
92     freopen("wormhole.in","r",stdin);
93     freopen("wormhole.out","w",stdout);
94     scanf("%d",&n);
95     for(int i=1;i<=n;i++)
96 {
97         scanf("%d%d",&e[i].x,&e[i].y);
98 }
99     sort(e+1,e+n+1,kong);
100     for(int i=1;i<n;i++)
101 {
102         if(e[i].y==e[i+1].y)afterr[i]=i+1;
103 }
104     dfs(1);
105     printf("%d
",ans);
106     return 0;
107 }

顺便附上传说中的官方标程,写的和我第二次的思路一样(第一次的思路也没错),但是比我的简洁很多

1 /*
2 ID:twd30651
3 PROG:wormhole
4 LANG:C++
5 */
6 //官方解答
7 #include <iostream>
8 #include <fstream>
9 using namespacestd;
10 #define MAX_N 12
11 
12 int N, X[MAX_N+1], Y[MAX_N+1];
13 int partner[MAX_N+1];
14 int next_on_right[MAX_N+1];
15 
16 bool cycle_exists(void)
17 {
18     for (int start=1; start<=N; start++) {
19         //does there exist a cylce starting from start
20         int pos =start;
21         for (int count=0; count<N; count++)
22 {
23             pos =next_on_right[partner[pos]];
24 }
25         if (pos != 0) return true;
26 }
27     return false;
28 }
29 
30 //count all solutions
31 int solve(void)
32 {
33     //find first unpaired wormhole
34     int i, total=0;
35     for (i=1; i<=N; i++)
36         if (partner[i] == 0) break;
37 
38     //everyone paired?
39     if (i >N) {
40         if (cycle_exists()) return 1;
41         else return 0;
42 }
43 
44     //try pairing i with all possible other wormholes j
45     for (int j=i+1; j<=N; j++)
46         if (partner[j] == 0) {
47             //try pairing i & j, let recursion continue to
48             //generate the rest of the solution
49             partner[i] =j;
50             partner[j] =i;
51             total +=solve();
52             partner[i] = partner[j] = 0;
53 }
54     returntotal;
55 }
56 
57 int main(void)
58 {
59     ifstream fin("wormhole.in");
60     fin >>N;
61     for (int i=1; i<=N; i++) fin >> X[i] >>Y[i];
62 fin.close();
63 
64     for (int i=1; i<=N; i++) //set next_on_right[i]...
65         for (int j=1; j<=N; j++)
66             if (X[j] > X[i] && Y[i] == Y[j]) //j right of i...
67                 if (next_on_right[i] == 0 ||
68                     X[j]-X[i] < X[next_on_right[i]]-X[i])
69                     next_on_right[i] =j;
70     //找出紧邻的右边平行点
71 
72     ofstream fout("wormhole.out");
73     fout << solve() << "";
74     //cout<<solve()<<endl;
75 fout.close();
76     return 0;
77 }

免责声明:文章转载自《USACO 1.3 Wormholes》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇如何正确使用HttpClient以及restTemplatelinux文件传输命令 scp下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

c语言中的原子操作

参考文章:https://blog.csdn.net/yikai2009/article/details/8650221 1. 原子操作:原子操作指的是在执行过程中不会被别的代码所中断的操作.。分为 位 和 整型变量 两类原子操作。 typedef struct { volatile int counter; } atomic_t; 2. 原子操作函数 分...

function类型(c++11)

1.c++五大可调用的对象   可调用的对象常常作为泛型算法的实参 1)函数 2)函数指针 函数名其实也是函数指针,只不过函数名是一个常量指针,它的值不能改变,只能指向该函数,不能改变它的值让它指向别的函数 void fun1(int a) { cout << a << endl; } void fun2(int a)...

重新整理 .net core 实践篇—————中间件[十九]

前言 简单介绍一下.net core的中间件。 正文 官方文档已经给出了中间件的概念图: 和其密切相关的是下面这两个东西: IApplicationBuilder 和 RequestDelegate(HttpContext context) IApplicationBuilder : public interface IApplicationBuilde...

unordered_map的哈希HASH重载——举例unordered_map与pair联合使用

有些时候,为了图省力,我们没准会这样的调用一个函数 unordered_map< pair<int, int>, int > mp; 但是很显然的是,这样的写法是会报错的,因为pair还没有HASH键值。 error: call to implicitly-deleted default constructor of 'std::...

(转)C++类所占内存大小计算

C++类所占内存大小计算 转载时请注明出处和作者联系方式文章出处:http://blog.csdn.net/chenchong08作者联系方式:vision_chen@yeah.net 说明:笔者的操作系统是32位的。 class A {}; sizeof( A ) = ?sizeof( A ) = 1明明是空类,为什么编译器说它是1呢?空类同样可以实例化...

Archivelog Completed Before VS UNTIL TIME

有网友在T.ASKMACLEAN.COM上 提问关于"DELETE ARCHIVELOG ALL COMPLETED BEFORE" 与 "DELETE ARCHIVELOG UNTIL TIME "的区别。为了了解这2个命令细微的差别,我们先来温习一些 ARCHIVED LOG的知识。   V$ARCHIVED_LOG: FIRST_TIME     ...