Pagini recente » Cod sursa (job #2286808) | Cod sursa (job #938528) | Cod sursa (job #1758872) | Cod sursa (job #296086) | Cod sursa (job #717181)
Cod sursa(job #717181)
#include <fstream>
#include <cstdio>
using namespace std;
#define nmax 100001
#define in "cautbin.in"
#define out "cautbin.out"
int n,a[nmax];
void act0()
{
int temp,pmax=0;
long i,s,mij;
scanf("%d",&temp);
i=1;
s=n;
while(i!=s && i<s)
{
mij=(i+s)/2;
if(pmax<mij && a[mij]==temp)
{
pmax=mij;
}
if ((temp==a[s] || temp==a[i]) &&i+1==s)
{
if(pmax<s && a[s]==temp)
{
pmax=s;
}
else if(pmax<i && a[i]==temp)
{
pmax=i;
}
break;
}
if(a[mij]<=temp)
{
i=mij;
}
else
{
s=mij;
}
}
printf("%d \n", pmax);
}
void act1()
{
int temp,pmax=0;
long i,s,mij;
scanf("%d",&temp);
i=1;
s=n;
while(i!=s && i<s)
{
mij=(i+s)/2;
if(pmax<mij && a[mij]<=temp)
{
pmax=mij;
}
if ((temp>=a[s] || temp>=a[i]) &&i+1==s)
{
if(pmax<s && a[s]<=temp)
{
pmax=s;
}
else if(pmax<i && a[i]==temp)
{
pmax=i;
}
break;
}
if(a[mij]<=temp)
{
i=mij;
}
else
{
s=mij;
}
}
printf("%d \n", pmax);
}
void act2()
{
int temp,pmax=-1;
long i,s,mij;
scanf("%d",&temp);
i=1;
s=n;
while(i!=s && i<s)
{
mij=(i+s)/2;
if((pmax>mij || pmax==-1) && a[mij]>=temp)
{
pmax=mij;
}
if ((temp<=a[s] || temp<=a[i]) && i+1==s)
{
if((pmax>s||pmax==-1) && a[s]>=temp)
{
pmax=s;
}
else if((pmax>i || pmax==-1) && a[i]>=temp)
{
pmax=i;
}
break;
}
if(a[mij]<temp)
{
i=mij;
}
else
{
s=mij;
}
}
printf("%d \n", pmax);
}
int main()
{
int m,i,act;
freopen(in,"r",stdin);
freopen(out,"w",stdout);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d", &m);
for(i=1;i<=m;i++)
{
scanf("%d", &act);
if(act==0)
act0();
else if(act==1)
act1();
else if(act==2)
act2();
}
return 0;
}