7-81 Least Prefix (10分)
7-81 Least Prefix (10分)
Given a non-empty array of N integers A, please find the smallest integer P such that all the numbers in A are in the subarray A[0..P].
Input Format:
A positive number N, followed by a list of N non-negative integers less than 1000000. N is no larger than 1000000.
Output Format:
The smallest integer P.
Sample Input:
5
2 2 1 0 1
Sample Output:
3
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxsize 1000001
#include<string.h>
#include<stdlib.h>
#define maxsize 1000001
int main()
{
int n;
int i;
scanf("%d",&n);
int temp;
int index;
int *a=malloc(sizeof(int)*(maxsize+1));
memset(a,0,maxsize*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",&temp);
if(a[temp]==0)
{
a[temp]=1;
index=i;
}
}
printf("%d\n",index);
return 0;
}
{
int n;
int i;
scanf("%d",&n);
int temp;
int index;
int *a=malloc(sizeof(int)*(maxsize+1));
memset(a,0,maxsize*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",&temp);
if(a[temp]==0)
{
a[temp]=1;
index=i;
}
}
printf("%d\n",index);
return 0;
}