Time limit: 1.0s memory limit: 256.0MB
Problem description
There are n ants on a long and slender pole 100 cm long. Some of their heads are to the left and some are to the right.
Each ant can only climb forward along the pole at a speed of 1 cm / s.
When two ants meet, they turn around and crawl in the opposite direction at the same time.
One of the ants has a cold. And when meeting with other ants, they will spread the cold to the ants they meet.
Please calculate how many ants have a cold when all ants climb off the pole.
Input format
In the first line, enter an integer n (1 < n < 50), representing the total number of ants.
The next line is n integers Xi (- 100 < Xi < 100) separated by spaces. The absolute value of Xi indicates the distance of the ant from the left end of the pole. A positive value indicates that the head is right, a negative value indicates that the head is left, and a value of 0 will not appear in the data, nor will two ants occupy the same position. Among them, the first data represents that the ant has a cold.
Output format
It is required to output an integer indicating the number of last cold ants.
sample input
3
5 -2 8
sample output
1
sample input
5
-10 8 -20 12 25
sample output
3
Analysis:
This problem is the deformation of poj 1852. When ants meet, if an ant has a cold, it will infect the other ant, and both ants will climb in the opposite direction. The difference between ants can be ignored.
Code:
#include <bits/stdc++.h>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define dec(i,n,a) for(int i=n;i>=a;i--)///[n,a]
#define pb push_back
#define fi first
#define se second
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double E=2.718281828459045;
const double eps=1e-3;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const int N=50+5;
const ll maxn=1e6+5;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int n,a[N];
int main()
{
//fre;
while(~scanf("%d",&n))
{
mem(a,0);
rep(i,1,n+1) scanf("%d",&a[i]);
if(a[1]<0)
{
int minx=100,cnt=0;
rep(i,2,n+1)
{
if(-a[1]>a[i]&&a[i]>0)
{
cnt++;
if(a[i]<minx)
minx=a[i];
}
}
rep(i,2,n+1)
{
if(minx==100)
break;
if(-a[i]>minx)
cnt++;
}
printf("%d\n",cnt+1);
}
else ///a[1]>0
{
int maxx=0,cnt=0;
rep(i,2,n+1)
{
if(a[1]<-a[i])
{
cnt++;
if(-a[i]>maxx)
maxx=-a[i];
}
}
rep(i,2,n+1)
{
if(maxx==0)
break;
if(a[i]<maxx&&a[i]>0)
{
cnt++;
}
}
printf("%d\n",cnt+1);
}
}
return 0;
}