牛客周赛 Round 42

小红叕战小紫

复制代码
#include "bits/stdc++.h"
using namespace std;

#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int> 
#define vi vector<int>
#define si set<int> 
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO  cout<<"No"<<endl;
#define pb(x) push_back(x);
#define fi first
#define sc second
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }

void solve()
{
	string s;
    cin>>s;
    if(s.size()>1){
        cout<<"kou";
    }
    else {
        cout<<"yukari";
    }
}

signed main()
{
	IOS
	int t;
	t=1;//cin>>t;
	while(t--){
		solve();
	}
}

小红的数组移动

复制代码
#include "bits/stdc++.h"
using namespace std;

#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int> 
#define vi vector<int>
#define si set<int> 
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO  cout<<"No"<<endl;
#define pb(x) push_back(x);
#define fi first
#define sc second
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int mod=1e9+7;
void solve()
{
	int n;
    cin>>n;
    vi a(n);
    for (int i=0;i<n;i++){
        cin>>a[i];
    }
    string s;
    cin>>s;
    int len=s.size();
    int sum=0;
    int pos=0;
    for (int i=0;i<len;i++){
        if(s[i]=='R'){
           // cout<<i<<endl;
            if(pos!=n-1){
                pos++;
                sum+=a[pos];
            }
            else {
                sum+=a[pos];
            }
        }
        else {
            //cout<<pos<<endl;
            if(pos!=0){
                pos--;
                sum+=a[pos];
            }
            else {
                sum+=a[pos];
            }
        }
        
    }
    cout<<sum%mod;;
}

signed main()
{
	IOS
	int t;
	t=1;//cin>>t;
	while(t--){
		solve();
	}
}

小红的素数合并

想要极差尽可能的小,在有偶数个数时,我们可以让首位相乘。

而在奇数时,我们可以空出最后一个数,因为相比空出中间那个数来说的话,最后一个数,不仅最小值变大了,而且最大值变小了。

复制代码
#include "bits/stdc++.h"
using namespace std;

#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int> 
#define vi vector<int>
#define si set<int> 
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO  cout<<"No"<<endl;
#define pb(x) push_back(x);
#define fi first
#define sc second
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int mod=1e9+7;
void solve()
{
	int n;
    cin>>n;
    vi a(n);
    for (int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(all(a));
    int maxn=0,minn=1e18;
    if(n%2==0){
        for (int i=0;i<n/2;i++){
            a[i]*=a[n-i-1];
            maxn=max(maxn,a[i]);
            minn=min(minn,a[i]);
        }
    }
    else {
        for (int i=0;i<n/2;i++){
            a[i]*=a[n-i-2];
            maxn=max(maxn,a[i]);
            minn=min(minn,a[i]);
        }
        maxn=max(maxn,a[n-1]);
        minn=min(minn,a[n-1]);
    }
    cout<<maxn-minn;
}

signed main()
{
	IOS
	int t;
	t=1;//cin>>t;
	while(t--){
		solve();
	}
}

小红的树上删边

树上问题熟悉的遍历树。当发现这个子树的联通块大小为偶数时,便可以切除一条边。

复制代码
#include "bits/stdc++.h"
using namespace std;

#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int> 
#define vi vector<int>
#define si set<int> 
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO  cout<<"No"<<endl;
#define pb(x) push_back(x);
#define fi first
#define sc second
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }

const int N = 200010;
int e[N],ne[N],st[N],h[N];
int idx;
void add(int a,int b){
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int ans=0;
int sz[N];
void dfs(int x){
    st[x]=1;
    sz[x]=1;
    for (int i=h[x];~i;i=ne[i]){
        int j=e[i];
        if(st[j]==0){
            dfs(j);
            sz[x]+=sz[j];
        }
    }
    if(x!=1 && sz[x]%2==0){
        ans++;
    }
    
}
void solve()
{
    int n;
    cin>>n;
    memset(h,-1,sizeof h);
    for (int i=1;i<=n-1;i++){
        int x,y;
        cin>>x>>y;
        add(x,y),add(y,x);
    }
     if(n&1){
        cout<<-1<<endl;
        return ;
    }
    dfs(1);
    cout<<ans;

}

signed main()
{
	IOS
	int t;
	t=1;//cin>>t;
	while(t--){
		solve();
	}
}

小红的子序列求和

使用了组合数的思想。j可以视为第i个数在序列中的位置。除去自身的贡献之外。还有前面j个数和后面j-j-1个数的贡献,并将组合数预处理,字符串的每一个字符的贡献只需要在这次计算就可以了。

复制代码
#include "bits/stdc++.h"
using namespace std;

#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int> 
#define vi vector<int>
#define si set<int> 
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO  cout<<"No"<<endl;
#define pb(x) push_back(x);
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }

const int INF =1e18;
const int N = 200010;
const int mod = 1e9+7;

//字符串的子序列有多少个。
int c[1010][1010];
int pow1[1000];

void init(){
    for (int i=0;i<=1000;i++){
        for (int j=0;j<=i;j++){
            if(j==0 || i==j){
                c[i][j]=1;
            }
            else{
                c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
            }
        }
    }
    pow1[0]=1;
    for (int i=1;i<=1000;i++){
        pow1[i]=pow1[i-1]*10%mod;
    }
}

void solve()
{
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    int ans=0;
    for (int i=0;i<n;i++){
        for (int j=0;j<k;j++){
            int tmp=(s[i]-'0');
            ans+=pow1[k-j-1]*tmp%mod*c[i][j]%mod*c[n-i-1][k-1-j]%mod;
            ans%=mod;
        }
    }
	cout<<ans;
}

signed main()
{
	IOS
    init();
	int t;
	t=1;//cin>>t;
	while(t--){
		solve();
	}
}
相关推荐
Y1nhl3 分钟前
力扣_链表_python版本
开发语言·python·算法·leetcode·链表·职场和发展
qq_4017004119 分钟前
C语言中位运算以及获取低8位和高8位、高低位合并
c语言·开发语言·算法
CoovallyAIHub22 分钟前
YOLO模型优化全攻略:从“准”到“快”,全靠这些招!
深度学习·算法·计算机视觉
闻缺陷则喜何志丹27 分钟前
【BFS】 P10864 [HBCPC2024] Genshin Impact Startup Forbidden II|普及+
c++·算法·宽度优先·洛谷
MicroTech20251 小时前
微算法科技(NASDAQ: MLGO)探索Grover量子搜索算法,利用量子叠加和干涉原理,实现在无序数据库中快速定位目标信息的效果。
数据库·科技·算法
今天背单词了吗9801 小时前
算法学习笔记:8.Bellman-Ford 算法——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·开发语言·后端·算法·最短路径问题
手握风云-1 小时前
优选算法的链脉之韵:链表专题
数据结构·算法·链表
Coding小公仔1 小时前
LeetCode 151. 反转字符串中的单词
开发语言·c++·算法
稳兽龙1 小时前
P1098 [NOIP 2007 提高组] 字符串的展开
c++·算法·模拟
G.E.N.2 小时前
开源!RAG竞技场(2):标准RAG算法
大数据·人工智能·深度学习·神经网络·算法·llm·rag