leetcode每日一题+Codeforces Round #787 (Div. 3)A~D

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

LeetCode每日一题(5.6)

933. 最近的请求次数

写一个 RecentCounter 类来计算特定时间范围内最近的请求。

请你实现 RecentCounter 类:

RecentCounter() 初始化计数器,请求数为 0 。
int ping(int t) 在时间 t 添加一个新请求,其中 t 表示以毫秒为单位的某个时间,并返回过去 3000 毫秒内发生的所有请求数(包括新请求)。确切地说,返回在 [t-3000, t] 内发生的请求数。
保证 每次对 ping 的调用都使用比之前更大的 t 值。

class RecentCounter {
    queue<int>q;
public:
    RecentCounter() {}
    int ping(int t) {
        q.push(t);
        while(t-3000>q.front())q.pop();
        return q.size();
    }
};

/**
 * Your RecentCounter object will be instantiated and called as such:
 * RecentCounter* obj = new RecentCounter();
 * int param_1 = obj->ping(t);
 */

Codeforces

A. Food for Animals

#include <bits/stdc++.h>
#define int long long
using namespace std;
int _;
void solve()
{
	int a,b,c,x,y;
	cin>>a>>b>>c>>x>>y;
	if(a>=x&&b>=y)
	{
		cout<<"YES"<<endl;
		return;
	}
	else if(a>=x)c-=y-b;
	else if(b>=y)c-=x-a;
	else if(a<x&&b<y)c-=(y-b)+(x-a);
	if(c>=0)cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
}
signed main()
{
	cin>>_;
	while(_--)solve();
	return 0;
} 

B - Make It Increasing

#include <bits/stdc++.h>
using namespace std;
#define int long long 
int _;
const int N=35;
int a[N];
int n;
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	int cnt=0;
	for(int i=n-1;i>=1;i--)
	{
		if(a[i]>=a[i+1])
		{
			while(a[i]>=a[i+1]&&a[i])a[i]/=2,cnt++;
			if(a[i]==0&&a[i+1]==0)
			{
				cout<<"-1"<<endl;
				return;
			}
		}
	}
	cout<<cnt<<endl;
}
signed main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

C - Detective Task

#include <bits/stdc++.h>
using namespace std;
#define int long long
int _;
int n;
void solve()
{
	string s;
	cin>>s;
	int a=0,b=0;
	int idx_a=-1,idx_b=-1;
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='1')
		{
			idx_b=i;
			b++;
		}
		else if(s[i]=='0')
		{
			if(idx_a==-1)idx_a=i;
			a++;
		}
	}
	if(a==0&&b==0)
	{
		cout<<s.size()<<endl;
	}
	else if(a==0)
	{
		cout<<s.size()-idx_b<<endl;
	}
	else if(b==0)
	{
		cout<<idx_a+1<<endl;
	}
	else if(a&&b)
	{
		cout<<idx_a-idx_b+1<<endl;
	}
}
signed main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

D - Vertical Paths

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
const int M=2*N;
int h[N],e[M],ne[M],idx;
vector<vector<int>>res;
vector<int>v;
int _;
int n;
void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u)
{
	v.push_back(u);
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		dfs(j);
	}
	if(h[u]==-1)
	{
		res.push_back(v);
		v.clear();
	}
}
void solve()
{
	cin>>n;
	res.clear();
	memset(h,-1,sizeof h);
	idx=0;
	int root=-1;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		if(x!=i)add(x,i);
		else root=x;
	}
	dfs(root);
	cout<<res.size()<<endl;
	for(auto x:res)
	{
		cout<<x.size()<<endl;
		for(auto y:x)cout<<y<<' ';
		cout<<endl;
	}
	cout<<endl;
}
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

leimingzeOuO

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

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

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

打赏作者

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

抵扣说明:

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

余额充值