当前位置: 首页 > article >正文

2025牛客寒假训练营1【代码】 A B D E G H J M

A.茕茕孑立之影

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 2e5 + 9;
const ll MOD = 1e9 + 7;
ll a[Q];
void solve(){
    ll n;cin>>n;
    for (ll i = 1; i <= n; i++)
    {
        cin>>a[i];
    }
    for (ll i = 1; i <= n; i++)
    {
        if(a[i]==1){
            cout<<-1<<"\n";
            return;
        }
    }
    cout<<MOD<<"\n";
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

B.一气贯通之刃

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 2e5 + 9;
const ll MOD = 1e9 + 7;
vector<ll> d[Q];
void solve(){
    ll n;cin>>n;
    for (ll i = 1; i < n; i++)
    {
        ll o,p;cin>>o>>p;
        d[o].push_back(p);
        d[p].push_back(o);
    }map<ll,ll> mp;
    ll l=0,r=0;
    for (ll i = 1; i <= n; i++)
    {
        mp[d[i].size()]++;
        if(d[i].size()==1){
            if(l==0) l=i;
            else r=i;
        }
    }
    if(mp[1]==2 and mp[2]==n-2){
        cout<<l<<" "<<r<<"\n";
    }else{
        cout<<-1<<"\n";
    }
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;//cin>>_;
    while(_--){
        solve();
    }
    return 0;
}c

D.双生双宿之决

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 2e5 + 9;
const ll MOD = 1e9 + 7;
ll a[Q];
void solve(){
    ll n;cin>>n;
    map<ll,ll> mp;
    for (ll i = 1; i <= n; i++)
    {
        cin>>a[i];mp[a[i]]++;
    }
    if(mp.size()==2){
        ll cnt=mp[a[1]];
        for(auto i:mp) if(i.second!=cnt){
            cout<<"No\n";
            return;
        }
    }else{
        cout<<"No\n";
            return;
    }
    cout<<"Yes\n";
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

E.双生双宿之错

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 2e5 + 9;
const ll MOD = 1e9 + 7;
ll a[Q];
void solve(){
    ll n;cin>>n;
    for (ll i = 1; i <= n; i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    ll l=n/2,r=l+1;
    ll lcnt=a[(l+1)/2],rcnt=a[(r+n)/2];
    ll ans=0,ans2=0,ans3=0;
    if(lcnt!=rcnt){
        for (ll i = 1; i <= l; i++)
        {
            ans+=abs(a[i]-lcnt);
        }
        for (ll i = r; i <= n; i++)
        {
            ans+=abs(a[i]-rcnt);
        }
        cout<<ans<<"\n";
    }else{
        for (ll i = 1; i <= l; i++)
        {
            ans2+=abs(a[i]-lcnt+1);//前半部分-1 因为前半部分永远比后半部分小
            ans3+=abs(a[i]-lcnt);
        }
        for (ll i = r; i <= n; i++)
        {
            ans2+=abs(a[i]-rcnt);
            ans3+=abs(a[i]-rcnt-1);//后半部分+1 后半部分永远比前半部分大
        }
        cout<<min(ans2,ans3)<<"\n";
    }
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

G.井然有序之衡

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 2e5 + 9;
const ll MOD = 1e9 + 7;
ll a[Q];
void solve(){
    ll n;cin>>n;ll cnt=0,sum=0,ans=0;
    map<ll,ll> mp;
    for (ll i = 1; i <= n; i++)
    {
        cnt+=i;
        cin>>a[i];mp[a[i]]++;
        sum+=a[i];
    }
    ll it=n;
    if(sum!=cnt){
        cout<<-1<<"\n";
    }else{
        sort(a+1,a+1+n);
        ll ans=0;
        for (ll i = 1; i <= n; i++)
        {
            ans+=abs(a[i]-i);
        }
        cout<<ans/2<<"\n";
    }
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;//cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

H.井然有序之窗

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 1e5 + 9;
const ll MOD = 1e9 + 7;
ll ans[Q];
struct op
{
    ll l,r,ct;
};
bool cmp(op l,op r){
    if(l.l==r.l) return l.r<r.r;
    else return l.l<r.l;
}
struct cmp2
{
    bool operator()(pair<ll,ll> l,pair<ll,ll> r){
        return l.first>r.first;
    }
};

op a[Q];
void solve(){
    ll n;cin>>n;
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,cmp2> pq;
    for (ll i = 1; i <= n; i++) cin>>a[i].l>>a[i].r,a[i].ct=i;
    sort(a+1,a+1+n,cmp);
    ll it=1;
    for (ll i = 1; i <= n; i++)
    {
        while(it<=n and a[it].l<=i){
            pq.push({a[it].r,a[it].ct});it++;
        }
        if(pq.size()==0){
            cout<<-1<<"\n";
            return;
        }
        pair<ll,ll> now=pq.top();
        pq.pop();
        if(now.first>=i){
            ans[now.second]=i;
        }
        else{
            cout<<-1<<"\n";
            return;
        }
    }
    for (ll i = 1; i <= n; i++)
    {
        cout<<ans[i]<<" ";
    }
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;//cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

J.硝基甲苯之袭

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 3e5 + 9;
const ll MOD = 1e9 + 7;
ll a[Q];
ll mp[Q];
void solve(){
    ll n;cin>>n;
    for (ll i = 1; i <= n; i++){
        cin>>a[i];mp[a[i]]++;
    }
    ll ans=0;//  x^y == gcd(x,y)
    for (ll i = 1; i <= 2e5; i++)//枚举x
    {
        for (ll j = 1; j * j <= i ; j++)//枚举x的因子 y=i+j,x=i;
        {
            if(i%j==0){//是因子
                if((i^(i+j))==gcd(i,(i+j))) ans+=mp[i]*mp[i+j];
                if(j!=i/j) //平方时只加一次
                if((i^(i+i/j))==gcd(i,(i+i/j))) ans+=mp[i]*mp[i+i/j];
            }
        }
    }
    cout<<ans;
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;//cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

M.数值膨胀之美

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 1e5 + 9;
const ll MOD = 1e9 + 7;
ll a[Q];
void solve(){
    ll n;cin>>n;
    ll l=1,r=1;
    multiset<ll> st;
    for (ll i = 1; i <= n; i++)
    {
        cin>>a[i];
        st.insert(a[i]);
        if(a[i]<a[l]){
            l=i,r=i;
        }
    }
    ll ans=1e18;
    st.erase(st.lower_bound(a[l]));
    st.insert(a[l]*2);
    ans=min(*st.rbegin()-*st.begin(),ans);
    l--,r++;
    while(l>=1 or r<=n){
        if(r>n or (a[l]<a[r] and l>=1)){
            st.erase(st.lower_bound(a[l]));
            st.insert(a[l]*2);
            l--;
            ans=min(*st.rbegin()-*st.begin(),ans);
        }else{
            st.erase(st.lower_bound(a[r]));
            st.insert(a[r]*2);
            r++;
            ans=min(*st.rbegin()-*st.begin(),ans);
        }
    }
    cout<<ans<<"\n";
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;//cin>>_;
    while(_--){
        solve();
    }
    return 0;
}


http://www.kler.cn/a/513368.html

相关文章:

  • Python 脚本-扫描当前目录和所有子目录并显示它们的大小。
  • PyTorch使用教程(13)-一文搞定模型的可视化和训练过程监控
  • 前端开发Web
  • Ubuntu 24.04 LTS 服务器折腾集
  • 【玩转全栈】----Django模板的继承
  • [JavaScript] 运算符详解
  • 线性回归笔记1-4
  • java依赖问题
  • VBA语言的区块链
  • 人工智能技术在冷链物流行业的应用前景
  • Java Web开发高级——单元测试与集成测试
  • Yearning开源MySQL SQL审核平台
  • 稳定的通信桥梁,CCLINKIE转ModbusTCP网关实现AGV运输的光速效应
  • 基于Python的多元医疗知识图谱构建与应用研究(上)
  • 基于Hadoop MapReduce的WordCount任务实现与部署
  • 什么是可信数据空间?有什么作用?
  • matlab构造线性相位FIR滤波器
  • 【算法】字符串之227.基本计算器 -- 双栈的变形
  • docker安装rabbitmq并启动测试页面
  • Spring AI PromptChatMemoryAdvisor
  • Docker导入镜像
  • 动手学深度学习11.6. 动量法-笔记练习(PyTorch)
  • golang网络编程
  • 【leetcode100】验证二叉搜索树
  • 【力扣:新动计划,编程入门 —— 题解 ①】
  • 重学SpringBoot3-WebClient配置与使用详解