挺简单的。
铺地毯:
裸裸2011noip提高day1,详见此。
参考程序:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=11000;
int x1[maxn],x2[maxn],y1[maxn],y2[maxn];
int n;
int main(){
freopen("carpet.in","r",stdin);
freopen("carpet.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i]);
int x0,y0;
scanf("%d%d",&x0,&y0);
int res=-1;
for (int i=n;i>0;i--)
if (x1[i]<=x0 && x0<=x1[i]+x2[i] && y1[i]<=y0 && y0<=y1[i]+y2[i]){
res=i;break;
}
printf("%d",res);
return 0;
}
冗余关系:
其实就是并查集,存在一个关系x,y就设f[y]=x,初始f[i]=i,然后压缩路径就行了。
参考程序:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=11000;
int f[maxn];
int n,m;
int find(int x){
return f[x]=f[x]==x?x:find(f[x]);
}
int main(){
freopen("relation.in","r",stdin);
freopen("relation.out","w",stdout);
scanf("%d%d",&m,&n);
for (int i=1;i<=n;i++)
f[i]=i;
int res=0;
for (int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
x=find(x),y=find(y);
if (x==y)res++;
f[y]=x;
}
printf("%d",res);
return 0;
}
等式:
很直白的模拟,但是数据很坑。
参考程序:
#include<cstdio>
#include<algorithm>
#include<cctype>
using namespace std;
int main(){
freopen("equal.in","r",stdin);
freopen("equal.out","w",stdout);
char cmd;
int a[3],j=0;
a[0]=a[1]=a[2]=-1;
bool ok=true,flag=true;
while (scanf("%c",&cmd)==1){
if (cmd=='=')break;
if (isdigit(cmd))a[j]=(a[j]==-1?0:a[j])*10+cmd-'0';
else if (cmd=='+')j++;else if (cmd=='-'){j++,ok=false;if (a[0]<0)flag=false;}
}
j++;
while (scanf("%c",&cmd)==1)
if (isdigit(cmd))a[j]=(a[j]==-1?0:a[j])*10+cmd-'0';
if (a[2]>-1)
if (ok)printf("%d",a[2]-max(a[0],a[1]));
else printf("%d",max(a[0],a[1])-(flag?1:-1)*a[2]);
else
if (ok)printf("%d",a[0]+a[1]);
else printf("%d",a[0]-a[1]);
return 0;
}
烘干机
然后也就没啥了。
参考程序:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=210000;
int a[maxn];
int n,k;
bool check(int x){
long long cnt=0;
for (int i=1;i<=n;i++){
int need=a[i]-x;
if (need>0)cnt+=need/k+(need%k>0);
}
return cnt<=x;
}
int main(){
freopen("kth.in","r",stdin);
freopen("kth.out","w",stdout);
scanf("%d%d",&n,&k);
int l=0,r=0;
for (int i=1;i<=n;i++){
scanf("%d",&a[i]);
r=max(r,a[i]);
}
k--;
if (k==0){printf("%d",*max_element(a+1,a+1+n));return 0;}
while (l<r){
int mid=(l+r)>>1;
if (check(mid))r=mid;
else l=mid+1;
}
printf("%d",l);
return 0;
}