From the track description follows that Maria moves the way that the water always located to the right from her, so she could fall into the water only while turning left. To check if the turn is to the left, let’s give every Maria’s moves directions a number: moving to the north — 0, moving to the west — 1, to the south — 2 and to the east — 3. Then the turn is to the left if and only if the number of direction after performing a turn dir is equal to the number before performing a turn oldDir plus one modulo 4.
This solution has complexity O(n).
1/* ***********************************************
2Author :111qqz
3Created Time :2016年03月31日 星期四 01时51分56秒
4File Name :code/cf/#346/D.cpp
5************************************************ */ 6 7#include<cstdio> 8#include<cstring> 9#include<iostream>10#include<algorithm>11#include<vector>12#include<queue>13#include<set>14#include<map>15#include<string>16#include<cmath>17#include<cstdlib>18#include<ctime>19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedeflonglongLL;25#define pi pair < int ,int >
26#define MP make_pair
2728usingnamespacestd;29constdoubleeps=1E-8;30constintdx4[4]={1,0,0,-1};31constintdy4[4]={0,-1,1,0};32constintinf=0x3f3f3f3f;33constintN=1005;34intn;35intdir[N];3637structnode38{39intx,y;40intgetdir(nodeb)41{42if(x==b.x)43{44if(y>b.y)return0;45elsereturn2;46}47else48{49if(x>b.x)return1;50elsereturn3;51}52}53}p[N];5455intmain()56{57#ifndef ONLINE_JUDGE
58freopen("code/in.txt","r",stdin);59#endif
60cin>>n;61ms(dir,-1);62for(inti=1;i<=n+1;i++)cin>>p[i].x>>p[i].y;63for(inti=1;i<=n;i++)64{65dir[i]=p[i].getdir(p[i+1]);66}6768intans=0;69for(inti=1;i<=n-1;i++)70{71if(dir[i]==(dir[i+1]+1)%4)ans++;72}73cout<<ans<<endl;7475#ifndef ONLINE_JUDGE
76fclose(stdin);77#endif
78return0;79}
以及还有一个O(1)的做法。。太神啦。。。
One can solve this problem in alternative way. Let the answer be equal to x (that means that the number of inner corners of 270 degrees equals x, but the number of inner corners of 90 degrees to n - x). As soon as the sum of the inner corners’ values of polygon of n vertices is equal to 180 × (n - 2), then_x_ × 270 + (n - x) × 90 equals to 180 × (n - 2). This leads us to, being the answer for the problem calculated in O(1).
http://acm.hdu.edu.cn/showproblem.php?pid=4451 题意:N clothes, M pants and K shoes,然后给出p个不合法的搭配,形式是“clothes x pants y” or “pants y shoes z”.” 问有多少种合法的方案。 思路:一开始觉得是容斥。。当然可以。。但是实际上,不合法的搭配的形式比较简单,每种不合法的发配都是两个两个的不合法,以及每种不合法的形式都有pants,那么我们就可以通过先确定pants,对于每种pants,方案数就是能和当前pants搭配的clothes数,乘以能和当前pants搭配的shoes数,然后累加每种pants的答案即可。