思路:大模拟
#include<stdio.h>
#include <stdlib.h>
#include<bits/stdc++.h>
#define itn int
using namespace std;
int main()
{
int mx[4]={-1,0,1,0};
int my[4]={0,1,0,-1};
int d1=0,d2=0;
char a[11][11];
int x1,x2,y1,y2;
for(int i=0;i<10;++i)
{
scanf("%s",&a[i]);
}
for (int i=0;i<10;++i)
{
for (int j=0;j<10;++j)
{
if (a[i][j]=='F')//F是人
{
x1=i,y1=j;
}
else if (a[i][j]=='C')
{
x2=i,y2=j;
}
}
}
bool visited[10][10][4][10][10][4] = {};
int time =0;
while (x1!=x2 || y1!=y2)
{
if (visited[x1][y1][d1][x2][y2][d2]) {
cout << "0";
return 0;
}
visited[x1][y1][d1][x2][y2][d2] = true;
int tx1=x1+mx[d1],ty1=y1+my[d1];
int tx2=x2+mx[d2],ty2=y2+my[d2];
if (tx1>=0 && tx1<10 && ty1>=0 && ty1<10 && a[tx1][ty1]!='*')
{
x1=tx1,y1=ty1;
}
else
{
d1=(d1+1)%4;
}
if (tx2>=0 && tx2<10 && ty2>=0 && ty2<10 && a[tx2][ty2]!='*')
{
x2=tx2,y2=ty2;
}
else
{
d2=(d2+1)%4;
}
time++;
}
cout<<time;
}
思路:简单二分,找到能满足题意的,最小长度
#include<bits/stdc++.h>
#define itn int
using namespace std;
long long n,m;
long long a[1000008];
int main()
{
cin>>n>>m;
for (int i=0;i<n;++i)cin>>a[i];
sort(a,a+n);
long long l=0,r=a[n-1];
while (l<=r)
{
long long sum=0;
long long mid=l+(r-l)/2;
for (int i=0;i<n;++i)
{
if (a[i]>mid) sum=sum+(a[i]-mid);
}
if (sum>=m)l=mid+1;
else if (sum<m) r=mid-1;
}
cout<<l-1;
return 0;
}
思路:二分
#include<stdio.h>
#include <stdlib.h>
#include<bits/stdc++.h>
#define itn int
using namespace std;
int n;
long long l=0,r=100000001,a[100005],k;
bool blbl(long long mid)
{
int ans=0;
for (int i=0;i<n;++i)
{
ans+=a[i]/mid;
}
if (ans>=k)return true;
else return false;
}
int main()
{
cin>>n>>k;
for (int i=0;i<n;++i)cin>>a[i];
sort(a,a+n);
while (l<r)
{
long long mid=l+(r-l)/2;
if (blbl(mid))l=mid+1;
else r=mid-1;
}
if (l==0)
{
cout<<0;
return 0;
}
cout<<l-1;
}