Codeforces Round #783 (Div. 2)

A

Direction Change

题意: 给定n*m的方格,每次向上下左右任意方向移动,但同一个方向不能连续走两次。开始位于(1,1),要走到(n,m),问最少多少步走到,无法走到输出-1

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
#define ios std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define PII pair<int,int>
typedef long long ll;
const int N=1e6+10;
const int inf=0x3f3f3f3f;

using namespace std;
int n,m;
void solve()
{
    cin>>n>>m;
    ll ans1=(min(n,m)-1)*2;
    if((n==1&&m>2)||(m==1&&n>2))
    {
        cout<<-1<<'\n';
        return ;
    }
    int p=(max(m,n)-min(m,n));
    if(p&1)
    {
        int t=1+((p+1)/2-1)*4;
        cout<<ans1+t<<'\n';
    }
    else 
    {
        int t=4+(p/2-1)*4;
        cout<<ans1+t<<'\n';
    }
}
int main()
{
    //ios;
    int _t=1;
    cin>>_t;
    while(_t--) solve();
    system("pause");
    return 0;
}

B

Social Distance

题意:有n个人,m把椅子,每个人有一个a[i],表示每个人希望他左边和右边各有连续a[i]把椅子是空的,问是否能安排下所有人

思路:贪心我们按照a[i]升序排序,尽可能让a[i]相差不大的数相邻,这样让空椅子发挥最大作用。按这种顺序安排位置,看最后剩余椅子数是否满足最后一个人的a[i] 

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
#define ios std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define PII pair<int,int>
typedef long long ll;
const int N=1e5+10;
const int inf=0x3f3f3f3f;

using namespace std;
int n,m;
int a[N];
void solve()
{
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    ll t=1;
    for(int i=1;i<n;i++)
    {
        t+=a[i]+1;
    }
    if(m-t>=a[n-1]) puts("YES");
    else puts("NO");
}
int main()
{
    //ios;
    int _t=1;
    cin>>_t;
    while(_t--) solve();
    system("pause");
    return 0;
}

C

Make it Increasing

题意:给定数组a,和全是0的数组b,每次操作可以让b[i]+=a[i]或者b[i]-=a[i],问最少需要多少次操作可以让数组b严格递增。

思路:最优解中一定有一个数未被操作,即0。若最终方案中有正有负,那么我们可以选择把最大负数或者最小正数变成0,即不操作这个数,那么总操作数肯定会减小;若最终方案中全为负数,那么可以选择将最大负数变为0;同理正数。所以最优解中一定有0,我们枚举0的位置,然后向左向右贪心即可。

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
#define ios std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define PII pair<int,int>
typedef long long ll;
const int N=5010;
const long long inf=0x3f3f3f3f3f3f3f3f;

using namespace std;
int n;
ll a[N];
ll b[N];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	ll ans=inf;
	for(int i=1;i<=n;i++)
	{
		ll tem=0;
		for(int j=1;j<=n;j++) b[i]=0;
		for(int j=i-1;j>=1;j--)
		{
			ll t=abs(b[j+1])/a[j]+1;
			b[j]=-t*a[j];
			tem+=t;
		}
		for(int j=i+1;j<=n;j++)
		{
			ll t=b[j-1]/a[j]+1;
			b[j]=t*a[j];
			tem+=t;
		}
		ans=min(ans,tem);
	}
	cout<<ans<<'\n';
}
int main()
{
	//ios;
	int _t=1;
	//cin>>_t;
	while(_t--) solve();
	system("pause");
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值