Codeforces Round #667 (Div. 3)
Codeforces Round #667 (Div. 3)
A. Yet Another Two Integers Problem
题意:给2个数a,b,每一步可a+k或a-k,1<=k<=10,问最少几步a变到b;
思路:差值除10,向上取整;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize ("unroll-loops")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#define INF 0x3f3f3f3f
#define lowbit(a) ((a)&-(a))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int t,n,a,b;
int main()
{
scanf("%d",&t);
while(t--)
{
cin>>a>>b;
if(a<b)
swap(a,b);
cout<<(a-b+9)/10<<endl;
}
}
B. Minimum Product
题意:给你a,b,x,y,n,最多可执行n步,每步可使a-1(但a不能小于x)或使b-1(但b不能小于y),问操作后a\(*\)b最小值
思路:分类讨论。当a+b和一样时,差值越大,a\(*\)b越小,所以将a减到最小再减b和b减到最小再减a,取较小的为答案。
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize ("unroll-loops")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#define INF 0x3f3f3f3f
#define lowbit(a) ((a)&-(a))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll t,n,a,b,x,y,ans;
int main()
{
scanf("%lld",&t);
while(t--)
{
cin>>a>>b>>x>>y>>n;
if(n>=a-x+b-y)
{
ans=x*y;
}
else if(n>=a-x&&n>=b-y)
{
ans=min(x*(b-(n-(a-x))),y*(a-(n-(b-y))));
}
else if(n>=a-x&&n<=b-y)
{
ans=min((b-n)*a,x*(b-(n-(a-x))));
}
else if(n>=b-y&&n<=a-x)
{
ans=min((a-n)*b,y*(a-(n-(b-y))));
}
else
{
ans=min((b-n)*a,(a-n)*b);
}
cout<<ans<<endl;
}
}
C. Yet Another Array Restoration
题意:构建一个n个数的等差数列含x,y(x<y),使等差数列最大值最小。
思路:枚举差值,找到第一个使x,y之间数的个数小于等于n的差值,则为答案数列的差值。若小于n则先往x前补数,再向y后补数。
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize ("unroll-loops")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#define INF 0x3f3f3f3f
#define lowbit(a) ((a)&-(a))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int t,n,x,y,num,q;
int main()
{
scanf("%d",&t);
while(t--)
{
num=0;
cin>>n>>x>>y;
for(int i=1;i<=100005;i++)
{
if((y-x)%i==0)
{
if(((y-x)/i+1)<=n)
{
num=i;
break;
}
}
}
//q=(x-1)/num;
for(int i=x;i<=y;i=i+num)
{
printf("%d ",i);
n--;
}
while(n&&x>=1)
{
x=x-num;
if(x>=1)
{
printf("%d ",x);
n--;
}
}
while(n)
{
y=y+num;
printf("%d ",y);
n--;
}
printf("\n");
}
}
D. Decrease the Sum of Digits
题意:给2个数n,s。问n最少要加几使n的各个位置的数相加小于等于s.
思路:用数组存储n每个位置的值,从最后一位开始,每次循环往前一位,若当前位置为0不操作,不为0则加到10,然后注意进位。每次用check检查是否满足条件,满足则输出。
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize ("unroll-loops")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#define INF 0x3f3f3f3f
#define lowbit(a) ((a)&-(a))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll t,n,s,ans;
int shu[20],len;
void solve(ll x)
{
memset(shu,0,sizeof(shu));
while(x>0)
{
shu[len++]=x%10;
x=x/10;
}
}
int check()
{
ll num=0;
for(int i=0;i<len;i++)
{
num=num+shu[i];
}
if(num<=s)
return 1;
return 0;
}
ll quickPower(ll a,ll b)
{
ll ans=1, p=a;
while(b>0)
{
if(b&1)
ans*=p;
p*=p;
b >>= 1;
}
return ans;
}
int main()
{
scanf("%lld",&t);
while(t--)
{
len=0;ans=0;
scanf("%lld%lld",&n,&s);
solve(n);
for(int i=0;i<len;i++)
{
if(i==len-1)
{
if(shu[i]==10)
{
printf("%lld\n",ans);
break;
}
else if(check())
{
printf("%lld\n",ans);
break;
}
else
{
ans=ans+(10-shu[i])*quickPower(10,i);
printf("%lld\n",ans);
break;
}
}
if(check())
{
printf("%lld\n",ans);
break;
}
if(shu[i]!=0)
{
ans=ans+(10-shu[i])*quickPower(10,i);
shu[i]=0;
shu[i+1]++;
for(int j=i+1;j<len;j++)
{
if(shu[j]==10)
{
shu[j]=0;
shu[j+1]++;
}
else
break;
}
}
}
}
}
补题
E. Two Platforms
题意:有n个点给x,y坐标,有2个长为k的平台,点不断下落问最多能接住几个点。
思路:平台可以放在无线下面,所以y坐标是没用的。先对点按x排序,用二分预处理出index[i]表示i-n空间中一个平台最多能接住几个点。然后第一个平台起始位置从1枚举到n,二分求出第一个平台的值,第二个平台的值用Index数组直接得出,相加后取最大值。
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize ("unroll-loops")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#define INF 0x3f3f3f3f
#define lowbit(a) ((a)&-(a))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int t,n,k,a[200005],x,index[200005];
int ans;
int main()
{
scanf("%d",&t);
while(t--)
{
ans=0;
memset(index,0,sizeof(index));
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>x;
sort(a+1,a+1+n);
for(int i=n;i>=1;i--)
{
int s=a[i]+k;
int j=upper_bound(a+1,a+1+n,s)-a;
index[i]=max(index[i+1],j-i);
}
for(int i=1;i<=n;i++)
{
int s=a[i]+k;
int j=upper_bound(a+1,a+1+n,s)-a;
ans=max(ans,j-i+index[j]);
}
cout<<ans<<endl;
}
}