题目描述
Some researchers are very interested in the peculiar dance these moon dancers perform on the full moon. It is marvelous to see how they dance around a circle, each dancer positioned and performing an exact set of moves that have been passed between dancers for generations. At some point of the dance, each of the N dancers sit at some point in the perimeter of the circle, no two dancers sit in the same place, and they start singing the song of times. Then, suddenly, a set of K dancers stand up and dance rotating counter clockwise around the circle until each of the K dancers meet with a dancer that is sitting, making K pairs of dancers, all of the K dancers will have rotated the same amount of degrees around the circle. At this point the K pairs continue with the dance while the N−2∗K (possibly none) dancers that are not matched continue singing the song of times.
Knowing your programming skills, researchers have come to you and ask for help on a very specific task: given the position of each of the N dancers when they sit at the perimeter of the circle, can you find the maximum number of pairs that can be matched to continue the dance?
输入
The first line contains a single integer N (2≤N≤360), representing the number of dancers in the tribe. The second and last line contains N integer numbers separated by a space, representing the angle in degrees ai (0≤ai<360) where the i-th dancer sits, it is guaranteed no two dancers will sit in the same place.
输出
Output a line containing a single integer. The maximum number of pairs that can be matched to continue the dance.
代码
#include <iostream>
#include <map>
#include<cstring>
using namespace std;
const int N=4000;
typedef pair<int,int>PII;
map<int,int>m;
int p[N];
int rec=0;
bool s[N],st[N],sk[N];
int ma=0;
bool find(int t,int y)
{
sk[t]=1;
int x=(t-y+360)%360;
if(!s[x]||sk[x])return 0;
if(!st[x]||find(x,y))
{
st[x]=1;
st[t]=1;
return 1;
}
return 0;
}
int main ()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>p[i],s[p[i]]=1;
for(int i=1;i<360;i++)
{
int sum=0;
m.clear();
memset(st,0,sizeof st);
for(int j=1;j<=n;j++)
{
if(!st[p[j]])
{
memset(sk,0,sizeof sk);
int t=(p[j]+i)%360;
if(s[t]&&!st[t])
{
sum++;
st[t]=1;
st[p[j]]=1;
}
else if(find(p[j],i))sum++;
}
}
rec=max(rec,sum);
}
cout<<rec<<endl;
}