首页 > 其他分享 >Codeforces Round #822 (Div. 2)

Codeforces Round #822 (Div. 2)

时间:2022-09-24 22:44:05浏览次数:74  
标签:int sum Codeforces Div 822 define

比赛链接

Codeforces Round #822 (Div. 2)

D. Slime Escape

给一个长度为 \(n\) 的序列以及一个 \(k\),表示当前位于 \(k\) 这个位置,要求该位置的数往左或者右,每次只能加上 \(a[i]\) 一次,且不允许出现负数,问最后能否到 \(0\) 或 \(n+1\)

解题思路

贪心

假设最后到达 \(0\),则每次向左时,一定可能是先向右补充,即必须先向右加上一些正数,再往左移动比较好,故每次往左移动时先看是否往右能补充

  • 时间复杂度:\(O(n)\)

代码

// Problem: D. Slime Escape
// Contest: Codeforces - Codeforces Round #822 (Div. 2)
// URL: https://codeforces.com/contest/1734/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=2e5+5;
int t,n,k,a[N];
vector<int> sum,need;
bool ck()
{
	sum.clear(),need.clear();
	int mn=0,s=0;
	for(int i=k+1;i<=n;i++)
	{
		s+=a[i];
		mn=min(mn,s);
		if(s>0)
		{
			sum.pb(s),need.pb(mn);
			mn=s=0;
		}
	}
	int res=a[k];
	for(int i=k,j=-1;i>=1;i--)
	{
		while(j+1<need.size()&&res+need[j+1]>=0)
		{
			j++;
			res+=sum[j];
		}
		if(res+a[i-1]<0)return false;
		res+=a[i-1];
	}
	return true;
}
signed main()
{
    for(cin>>t;t;t--)
    {
    	cin>>n>>k;
    	for(int i=1;i<=n;i++)cin>>a[i];
    	bool f=false;
    	if(ck())f=true;
    	reverse(a+1,a+1+n);
    	k=n-k+1;
    	if(ck())f=true;
    	puts(f?"YES":"NO");
    }
    return 0;
}

标签:int,sum,Codeforces,Div,822,define
From: https://www.cnblogs.com/zyyun/p/16726893.html

相关文章

  • Codeforces Round #822 (Div. 2) D
    https://codeforces.com/contest/1734/problem/D题意有n只史莱姆,每只都有一个值,其中第k只被你控制,你希望能走到0或n+1这两个位置,也就是说遇到路上的史莱姆需要......
  • CodeForces 比赛记录
    带星号的表示vp。\(*\)CFRound601Div.1做出A和B1。B2.SendBoxestoAlice(HardVersion)考虑\(a\)的前缀和数列\(S\),在\(a\)中移动一个数,相当于在\(S......
  • Codeforces Round #822 D,E
    E.RectangularCongruence我们考虑对ar1,c1+ar2,c2≢ar1,c2+ar2,c1(modn)(同余情况下不同也是可以同时加任意数的可以感性理解一下)ar1,c1-ar1,c2≢ar2,c1......
  • CF Round 822 Div2 题解
    比赛链接A题SelectThreeSticks(签到)给定\(n\)根木棒,第\(i\)根木棒的长度为\(a_i\)。现在我们可以进行操作,每次操作选定一根木棒,将其长度增高或减少1。问至少需......
  • Codeforces Round #822 Div.2 整场题解
    目前还有E,F没有更新。A.SelectThreeSticks直接对\(a\)排序,选出来的木棍一定是相邻三项,都往中间靠更优。B.Bright,Nice,Brilliant最优方案就是每一行第一个......
  • Codeforces Round #640 (Div. 4) D. Alice, Bob and Candies
    CodeforcesRound#640(Div.4)翻译岛田小雅D.Alice,BobandCandies出题人MikeMirzayanov\(n\)个糖果排成一排,从左到右分别被编号为\(1\)到\(n\)。第\(i\)......
  • Roadside Trees (Simplified Edition) CodeForces - 265B
    RoadsideTrees(SimplifiedEdition)CodeForces-265B松鼠Liss喜欢坚果。一条街上有n棵树(从西到东编号为1到n),每棵树的顶部都有一颗美味的坚果。树的高度我很高。......
  • Nikita and string CodeForces - 877B
    NikitaandstringCodeForces-877B有一个全由a和b组成的字符串,可以切成三部分。满足如下规则:第一部分只包含a或者是空串。第二部分只包含b或者是空串。第三部分只......
  • Barrels CodeForces - 1430B
    BarrelsCodeForces-1430B你有n个桶放在一排,从左到右开始编号分别为1~n.最初,第i桶装的水是ai升.你可以把水从一个桶倒到另一个桶。在这个过程中,你可以......
  • Fair Numbers CodeForces - 1465B
    FairNumbersCodeForces-1465B我们定义一个好数规则如下:它能够整除自己的每一个非零位。例如说,102是一个好数,因为它能整除1和2。282则不是,因为它不能整除8。......