暑假算法训练day11(力扣周赛+cfdiv3(A~F))

目录

  • LeetCode 2335. 装满杯子需要的最短总时长
  • LeetCode 2336. 无限集中的最小数字
  • LeetCode 2337. 移动片段得到字符串
  • LeetCode 2338. 统计理想数组的数目
  • A. Round Down the Price
  • B. Polycarp Writes a String from Memory
  • C. Train and Queries
  • D. Not a Cheap String
  • E. Split Into Two Sets
  • F. Equate Multisets

LeetCode 2335. 装满杯子需要的最短总时长

每次选取两个杯子,最后加上剩下的杯子

class Solution {
public:
    int fillCups(vector<int>& amount) {
        int res=0;
        sort(amount.begin(),amount.end(),greater<int>());
        while(amount[1]>0)
        {
            amount[0]--,amount[1]--;
            res++;
            sort(amount.begin(),amount.end(),greater<int>());
        }
        res+=amount[0];
        return res;
    }
};

LeetCode 2336. 无限集中的最小数字

模拟一下unordered_set

class SmallestInfiniteSet {
public:
    unordered_set<int>s;
    SmallestInfiniteSet() {

    }

    int popSmallest() {
        for(int i=1;;i++)
        {
            if(!s.count(i))
            {
                s.insert(i);
                return i;
            }    
        }
    }

    void addBack(int num) {
        s.erase(num);
    }
};

/**
 * Your SmallestInfiniteSet object will be instantiated and called as such:
 * SmallestInfiniteSet* obj = new SmallestInfiniteSet();
 * int param_1 = obj->popSmallest();
 * obj->addBack(num);
 */

LeetCode 2337. 移动片段得到字符串

这题差不多是个原题。
首先LR的相对位置是不变的,所以我们可以先去掉_,判断两个字符串是否相同,不同直接返回false
然后用原字符串进行判断,如果i不等于j,如果start[i]='L'在target的左边那么没办法移过去,同理R在右边也不可以

class Solution {
public:
    bool canChange(string start, string target) {
        string a=start,b=target;
        a.erase(remove(a.begin(),a.end(),'_'),a.end());
        b.erase(remove(b.begin(),b.end(),'_'),b.end());
        if(a!=b)return false;
        for(int i=0,j=0;i<start.size();i++)
        {
            if(start[i]=='_')continue;
            while(target[j]=='_')j++;
            if(i!=j)
            {
                if(start[i]=='L'&&i<j)return false;
                if(start[i]=='R'&&i>j)return false;
            }
            j++;
        }
        return true;
    }
};

LeetCode 2338. 统计理想数组的数目

maxvalue的最大值 1 0 4 10^4 104,所以一个理想数组的个数最多有 l o g 1 0 4 + 1 = 14 log{10^4}+1=14 log104+1=14
爆搜

class Solution {
public:
    int n,m;
    int res=0;
    const int MOD=1e9+7;
    vector<vector<int>>f;
    void dfs(int u,int cnt)
    {
        res=(res+f[n-1][cnt-1])%MOD;
        if(cnt<n)
        {
            for(int i=2;i*u<=m;i++)
            {
                dfs(i*u,cnt+1);
            }
        }
    }
    int idealArrays(int n, int m) {
        this->n=n,this->m=m;
        f=vector<vector<int>>(n,vector<int>(20));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<20&&j<=i;j++)
            {
                if(!j)f[i][j]=1;
                else f[i][j]=(f[i-1][j]+f[i-1][j-1])%MOD;
            }
        }
        for(int i=1;i<=m;i++)
            dfs(i,1);
        return res;
    }
};

A. Round Down the Price

#include 
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#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;
void solve()
{
	cin>>n;
	int wei=0;
	int m=n;
	while(m)
	{
		m/=10;
		wei++;
	} 
	printf("%lld\n",n-(int)pow(10,wei-1));
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

B. Polycarp Writes a String from Memory

#include 
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#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;
void solve()
{
	string s;
	cin>>s;
	int res=0;
	map<char,int>mp;
	for(int i=0;i<s.size();i++)
	{
		if(mp.size()<3)
		{
			mp[s[i]]++;
		}
		else
		{
			if(!mp.count(s[i]))
			{
				res++;
				mp.clear();
				mp[s[i]]++;
			}
		}
	}
	if(mp.size())res++;
	cout<<res<<endl;
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

C. Train and Queries

#include 
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#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,k;
const int N=2e5+10;
int a[N];
void solve()
{
	cin>>n>>k;
	map<int,vector<int>>mp;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		mp[a[i]].pb(i);
	}
	while(k--)
	{
		int l,r;
		cin>>l>>r;
		if(!mp.count(l)||!mp.count(r))
		{
			cout<<"NO"<<endl;
			continue;
		}
		int x=0,y=0;
		for(auto t:mp[l])
		{
			x=t;
			break;
		}
		for(auto t:mp[r])
		{
			y=t;
		}
		if(x>=y)cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	} 
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

D. Not a Cheap String

#include 
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#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 k;
void solve()
{
	string s;
	cin>>s;
	cin>>k;
	int cost=0;
	map<char,int>mp;
	for(auto x:s)
		mp[x]++,cost+=x-'a'+1;
	if(cost<=k)
	{
		cout<<s<<endl;
		return;
	}
	for(int i=25;i>=0;i--)
	{
		while(mp.count(i+'a')&&mp[i+'a']&&cost>k)
		{
			mp[i+'a']--;
			cost=cost-i-1;
		}
	}
	for(auto x:s)
	{
		if(mp[x])
		{
			cout<<x;
			mp[x]--;
		}
	}
	cout<<endl;
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

E. Split Into Two Sets

#include 
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#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 f[N],cnt[N];
int st[N];
int find(int x)
{
	if(x!=f[x])f[x]=find(f[x]);
	return f[x];
}
void unite(int x,int y)
{
	int a=find(x),b=find(y);
	if(a!=b)
	{
		cnt[a]+=cnt[b];
		f[b]=a;
	}
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)f[i]=i,cnt[i]=1,st[i]=0;
	for(int i=1;i<=n;i++)
	{
		int x,y;
		cin>>x>>y;
		st[x]++,st[y]++;
		unite(x,y);
	}
	bool s=false;
	for(int i=1;i<=n;i++)
	{
		if(st[i]!=2)
		{
			s=true;
			break;
		}
	}
	if(s)
	{
		cout<<"NO"<<endl;
		return;
	}
	for(int i=1;i<=n;i++)
	{
		if(f[i]==i)
		{
			if(cnt[i]%2)
			{
				cout<<"NO"<<endl;
				return;
			}
		}
	}
	cout<<"YES"<<endl;
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

F. Equate Multisets

#include 
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#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],b[N];
void solve()
{
	cin>>n;
	map<int,int>mp;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		while(a[i]%2==0)a[i]/=2;
		mp[a[i]]++;
	}
	for(int i=1;i<=n;i++)
	{
		cin>>b[i];
		while(b[i]%2==0)b[i]/=2;
	}
	sort(b+1,b+1+n);
	bool f=false;
	for(int i=1;i<=n;i++)
	{
		while(b[i]&&mp[b[i]]==0)b[i]/=2;
		if(b[i]==0)
		{
			f=true;
			break;
		}
		else mp[b[i]]--;
	}
	if(f)cout<<"NO"<<endl;
	else cout<<"YES"<<endl;
}
signed main()
{
    io;
 	cin>>_; 
	while(_--)
    solve();
    return 0;
}

你可能感兴趣的:(codeforces,C++,LeetCode,leetcode,算法,数据结构,codeforces)