Pagini recente » Cod sursa (job #36645) | Cod sursa (job #1210372) | Cod sursa (job #495500) | Cod sursa (job #1897976) | Cod sursa (job #717239)
Cod sursa(job #717239)
#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;
do
{
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+1;
}
else if(a[mij]>temp)
{
s=mij-1;
}
else
{
pmax=mij;
i=mij+1;
}
}
while(i<=s);
printf("%d \n", pmax);
}
void act1()
{
int temp,pmax=0;
long i,s,mij;
scanf("%d",&temp);
i=1;
s=n;
do
{
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)
{
pmax=mij;
i=mij+1;
}
else
{
s=mij-1;
}
}
while(i<=s);
printf("%d \n", pmax);
}
void act2()
{
int temp,pmax=-1;
long i,s,mij;
scanf("%d",&temp);
i=1;
s=n;
do
{
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+1;
}
else
{
pmax=mij;
s=mij-1;
}
}while(i<=s);
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;
}