Problem Description
The Ferries Wheel is a circle,rounded by many cable cars,and the cars are numbered
1,2,3...K−1,K
in order.Every cable car has a unique value and
A[i−1]<A[i]<A[i+1](1<i<K)
.
Today,Misaki invites
N
friends to play in the Ferries Wheel.Every one will enter a cable car. One person will receive a kiss from Misaki,if this person satisfies the following condition: (his/her cable car's value + the left car's value) % INT_MAX = the right car's value,the
1
st
car’s left car is the
k
th
car,and the right one is
2
nd
car,the
k
th
car’s left car is the
(k−1)
th
car,and the right one is the
1
st
car.
Please help Misaki to calculate how many kisses she will pay,you can assume that there is no empty cable car when all friends enter their cable cars,and one car has more than one friends is valid.
Input
There are many test cases.
For each case,the first line is a integer
N(1<=N<=100)
means Misaki has invited
N
friends,and the second line contains
N
integers
val1,val2,...valN
, the
val[i]
means the
i
th
friend's cable car's value.
(0<=val[i]<=
INT_MAX).
The INT_MAX is
2147483647
.
Output
Sample Input
3
1 2 3
5
1 2 3 5 7
6
2 3 1 2 7 5
Sample Output
Case #1: 1
Case #2: 2
Case #3: 3
这个题目带坑的一点是一个car里可以有几个小朋友,不过这也不算什么,基本就是暴力,还有要注意就是INT_MAX is
2147483647
,当时就是这里没注意,用了int就罪过罪过了,改long long 就没问题了。
上代码
#include <iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
class car
{
public:
long long int v;
int ct;
};
long long int person[110];
long long int maxint=2147483647;
int n,k,cs;
int main()
{
cs=1;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%I64d",&person[i]);
}
sort(person,person+n);
car cars[110];
k=0;
for(int i=0;i<n;i++)
{
if(i==0&&k==0)
{
cars[k].v=person[i];
cars[k].ct=1;
k++;
}else
{
if(person[i]==cars[k-1].v)
{
cars[k-1].ct++;
}else
{
cars[k].v=person[i];
cars[k].ct=1;
k++;
}
}
}
if(k==1)
{
printf("Case #%d: -1\n",cs);
}else
{
int cnt=0;
for(int i=0;i<k;i++)
{
if((cars[((i-1+2*k)%k)].v+cars[i].v)%maxint==cars[(i+1)%k].v)
{
cnt+=cars[i].ct;
}
}
printf("Case #%d: %d\n",cs,cnt);
}
cs++;
}
//cout << "Hello world!" << endl;
return 0;
}