hdu 4151 4146 4145
View Code
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAX = 800000;
int a[800000];
bool is(int x)
{
int i=0,a[11];
memset(a,0,sizeof(a));
while(x>0)
{
int tmp=x%10;
a[tmp]++;
if(a[tmp]>=2) return false;
x/=10;
}
return true;
}
int tot=0;
void init()
{
int i;
for(i=1;i<=10000000;i++)
{
if(is(i))
{
a[++tot]=i;
}
}
}
int bs(int num)
{
int l=1,r=tot;
int mid;
while(l<=r)
{
mid=(l+r)>>1;
if(a[mid]>=num) r=mid-1;
else
{
l=mid+1;
if(a[l]>=num) return l;
}
}
}
int main()
{
int n;
int i,j,k;
init();
while(scanf("%d",&n)!=EOF)
{
int ans;
if(n<0) ans=0;
else
{
ans=bs(n);
// if(is(n))
ans--;
}
printf("%d\n",ans);
}
return 0;
}
做傻了,加了个树状数组多此一举
View Code
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAX = 800000;
int c[800000];
int a[800000];
bool is(int x)
{
int i=0,a[11];
memset(a,0,sizeof(a));
while(x>0)
{
int tmp=x%10;
a[tmp]++;
if(a[tmp]>=2) return false;
x/=10;
}
return true;
}
int tot=0;
void init()
{
int i;
for(i=1;i<=10000000;i++)
{
if(is(i))
{
// flag[i]=true;
a[++tot]=i;
}
}
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int d)
{
while(x<=MAX)
{
c[x]+=d;
x+=lowbit(x);
}
}
int sum(int x)
{
int ans=0;
while(x>0)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
int bs(int num)
{
int l=1,r=tot;
int mid;
while(l<=r)
{
mid=(l+r)>>1;
if(a[mid]>=num) r=mid-1;
else
{
l=mid+1;
if(a[l]>=num) return l;
}
}
// return mid;
}
int main()
{
int n;
int i,j,k;
init();
memset(c,0,sizeof(c));
for(i=1;i<=tot;i++)
{
update(i,1);
}
while(scanf("%d",&n)!=EOF)
{
int ans;
if(n<0) ans=0;
else
{
int id;
id=bs(n);
id--;
// printf("id=%d\n",id);
ans=sum(id);
}
printf("%d\n",ans);
}
return 0;
}
4145
先排序,再枚举
View Code
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct point {
double x,y;
}p[100010],a,b;
double dis(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cmp(point x,point y)
{
return dis(x,a)<dis(y,a);
}
double max(double a,double b)
{
return a>b?a:b;
}
double min(double a,double b)
{
return a<b?a:b;
}
double aa[100010];
int main()
{
int t;
int i,j,n;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
double ans=1000000000;
sort(p+1,p+n+1,cmp);
aa[n]=dis(p[n],b);
for(i=n-1;i>=1;i--)
{
aa[i]=max(dis(p[i],b),aa[i+1]);
// printf("i=%d aa=%.0lf\n",i,aa[i]);
}
double tmp=aa[1];//printf("tmp=%.0lf\n",tmp);
for(i=1;i<n;i++)
{
tmp=min(tmp,aa[i+1]+dis(p[i],a));
}
tmp=min(dis(p[n],a),tmp);
printf("%.0lf\n",tmp);
}
}
4146
记录每个点改变次数的奇偶性即可,另外还可以开挂,就快个800ms吧,呵呵
View Code
#include<stdio.h>
#include<string.h>
int num1[1010],num2[1010];
char s[1010][1010];
int get_val()
{
int ret(0);
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'0';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*10+c-'0';
return ret;
}
int main()
{
int t,cases=1;
int i,j,n,x,y,q;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%s",s[i]+1);
scanf("%d",&q);
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
while(q--)
{
x=get_val();
y=get_val();
num1[x]++;
num2[y]++;
}
int ans=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(s[i][j]=='w'&&(num1[i]+num2[j])%2==0)
ans++;
else if(s[i][j]=='b'&&(num1[i]+num2[j])%2==1)
ans++;
}
}
printf("Case #%d: %d\n",cases++,ans);
}
}