Codeforces Round #802 (Div. 2)A~D

115 篇文章 3 订阅
42 篇文章 2 订阅

A. Optimal Path

思路:
先走到最右端再往下走

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
void solve()
{
	cin>>n>>m;
	int sum=0;
	sum+=m*(1+m)/2;
	sum+=n*m+n*(n-1)*m/2;
	cout<<sum-m<<endl;
}
signed main()
{
    io;
  	cin>>_; 
 	while(_--)
    solve();
    return 0;
}

B. Palindromic Numbers

思路:第一位小于9的补成 9…99,否则补成1000…1
高精度减法

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n; 
vector<int>sub(vector<int>& A, vector<int>& B)
{
    vector<int>C;
    int t = 0;
    for (int i = 0; i < A.size(); i++)
    {
        t = A[i] - t;
        if (i < B.size())t -= B[i];
        C.push_back((t + 10) % 10);
        if (t < 0)t = 1;
        else t = 0;
    }
    while (C.size() > 1 && C.back() == 0)C.pop_back();
    return C;
}
void solve()
{
	string a,b;
	cin>>n;
	cin>>a;
	if(a[0]=='9')
	{
		for(int i=0;i<=n;i++)b+='1';
	}
	else 
	{
		for(int i=1;i<=n;i++)b+='9';
	}
	vector<int>A,B,C;
    for (int i = n - 1; i >= 0; i--)A.push_back(a[i] - '0');
    for (int i = b.size()-1 ; i >= 0; i--)B.push_back(b[i] - '0');
    C=sub(B,A);
    for(int i=C.size()-1;i>=0;i--)cout<<C[i];
    cout<<endl;
}
signed main()
{
    io;
  	cin>>_; 
 	while(_--)
    solve();
    return 0;
}

C. Helping the Nature

思路:对于差分数组,
操作一:d1减1,di+1加1
操作二:di加1
操作三:d1 加1
让差分数组除了第一位变成0通过操作1和2 ,然后对差分数组第一位进行操作3

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n; 
const int N=200010;
int a[N],b[N];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=a[i]-a[i-1];
	}
	int sum=0;
	for(int i=2;i<=n;i++)
	{
		if(b[i]>0)
		{
			sum+=b[i];
			b[i]=0;
		}
		else if(b[i]<0)
		{
			sum-=b[i];
			b[1]+=b[i];
			b[i]=0;
		}
	}
	sum+=abs(b[1]);
	cout<<sum<<endl;
}
signed main()
{
    io;
  	cin>>_; 
 	while(_--)
    solve();
    return 0;
}

D. River Locks

思路:二分+前缀和
所有水闸都打开不能满足,则输出-1
二分答案

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=2e5+10;
int a[N]; 
int s[N]; 
bool check(int cnt,int x)
{
	return x*cnt>=s[n];
}
void solve()
{
	cin>>n;
	int maxv=0;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)
	{
		s[i]=s[i-1]+a[i];
		maxv=max(maxv,(int)ceil(s[i]*1.0/i));
	}
	int q;
	cin>>q;
	while(q--)
	{
		int x;
		cin>>x;
		if(x<maxv)
		{
			cout<<-1<<endl;
			continue;
		}
		else
		{
			int l=1,r=n;
			while(l<r)
			{
				int mid=l+r>>1;
				if(check(mid,x))r=mid;
				else l=mid+1;
			}
			cout<<r<<endl;
		}	
	}
}
signed main()
{
    io;
  	//cin>>_; 
 	//while(_--)
    solve();
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值