目录
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;
}