AtCoder Beginner Contest 376

A - Candy Button

思路

模拟

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    using namespace std;
    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef priority_queue<int> PQ;

    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int n,t;
    int a[N];

    signed main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>n>>t;
        int ans=1;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        int sum=0;
        for(int i=2;i<=n;i++){
            sum+=a[i]-a[i-1];
            if(sum>=t){
                ans++;
                sum=0;
            }
        }
        cout<<ans<<endl;
        


        return 0;
    }

B - Hands on Ring (Easy)

思路

模拟过程,分类讨论即可,先分左右手,再分目标位置计算出正逆时针的距离,最后根据另外一只手的位置选择;

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    using namespace std;
    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef priority_queue<int> PQ;

    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int n,q;
    int l,r;
    string s;
    int p;
    int ans=0;

    signed main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>n>>q;
        l=1;
        r=2;
        for(int i=1;i<=q;i++){
            cin>>s>>p;
            if(s=="R"){
                int d1,d2;d1=d2=0;
                if(p>=r){
                    d1=p-r;
                    d2=n-d1;
                    if(l>r&&l<p)ans+=d2;
                    else ans+=d1;
                }
                else{
                    d1=r-p;
                    d2=n-d1;
                    if(l>p&&l<r)ans+=d2;
                    else ans+=d1;
                }
                r=p;
            }
            else{
                int d1,d2;d1=d2=0;
                if(p>=l){
                    d1=p-l;
                    d2=n-d1;
                    if(r>l&&r<p)ans+=d2;
                    else ans+=d1;
                }
                else{
                    d1=l-p;
                    d2=n-d1;
                    if(r>p&&r<l)ans+=d2;
                    else ans+=d1;
                }
                l=p;
            }
        }
        cout<<ans<<endl;
        return 0;
    }

C - Prepare Another Box

思路

答案具有可二分性,找大值中的最小值,则二分答案,$ check() $ 函数排序贪心方案是否可行即可;

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    using namespace std;
    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef priority_queue<int> PQ;

    const int N = 2e5+10, MAX = 1e9 + 10, INF = -1e9;

    int n;
    int a[N];
    int b[N];
    int e[N];

    bool check(int x){
        for(int i=1;i<=n-1;i++)e[i]=b[i];
        e[n]=x;
        sort(e+1,e+1+n);
        for(int i=1;i<=n;i++){
            if(a[i]>e[i]){
                return false;
            }
        }
        return true; 

    }

    signed main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        sort(a+1,a+1+n);
        for(int i=1;i<=n-1;i++){
            cin>>b[i];
        }
        int l=0;int r=MAX;
        while(l<r){
            int mid=(l+r)>>1;
            if(check(mid))r=mid;
            else l=mid+1;
            //cout<<l<<" "<<r<<endl;
        }
        if(l==0||l==MAX)cout<<-1<<endl;
        else cout<<l<<endl;

        return 0;
    }

D - Cycle

思路

$ N $ 很大,不能用DFS,仔细思考,最短环的长度就是由 \(1\) 节点到倒数第二个节点的最短距离加1,所以BFS出 \(1\) 节点到每个节点的最短距离$ dist[i] $,答案就是直接指向 \(1\) 节点的所有节点中最小值+1;
\(ans=min(ans,diat[i]+1)\)(其中 \(i\) 直接指向 \(1\));

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    using namespace std;
    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef priority_queue<int> PQ;

    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int n,m;
    int u,v;
    map<int,set<int>> mp;
    vector<int> fa;
    queue<int> q;
    bool st[N];
    int dist[N];
    int ans=MAX;

    void bfs(){
        q.push(1);
        st[1]=true;
        dist[1]=0;

        while(!q.empty()){
            int p=q.front();
            q.pop();
            for(auto i:mp[p]){
                if(!st[i]){
                    st[i]=true;
                    dist[i]=dist[p]+1;
                    q.push(i);
                }
            }
        }
        return ;
    }

    signed main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>n>>m;
        for(int i=1;i<=m;i++){
            cin>>u>>v;
            mp[u].insert(v);
            if(v==1)fa.pb(u);
        }
        memset(dist,0x3f,sizeof dist);
        bfs();
        for(auto i:fa){
            ans=min(ans,dist[i]+1);
        }

        if(ans<MAX)cout<<ans<<endl;
        else cout<<"-1"<<endl;

        return 0;
    }

E - Max × Sum

思路

枚举表达式中的$ max( A[i] )$,对 \(A\) 数组排序,$ B $ 数组跟着排序,这样表达式的第二个部分就一定在$ 1~i $ 之间,遍历过程中需要不断插入删除,找到最小值,用堆优化(优先队列);时间复杂度为 \(O(nlogn)\);

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    const int N = 2e5+10, MAX = 1e18 + 10, INF = -1e9;

    void solve(){
        int n, k;
        PQ pq;
        cin>>n>>k;
        vector<int> a(n), b(n), id(n);
        for (auto& i : a)
            cin >> i;
        for (auto& i : b)
            cin >> i;
        iota(id.begin(), id.end(), 0);
        sort(id.begin(), id.end(), [&](int x, int y) { return a[x] < a[y]; });

        int sum = 0; int ans = MAX;
        for (int j = 0; j < n; ++j) {
            int i = id[j];
            while (pq.size() > k) {
                sum-=pq.top();
                pq.pop();
            }
            if (j >= k - 1) {
            int del = (j >= k ? pq.top() : 0);
            ans = min(ans, 1ll * a[i] * (sum - del + b[i]));
            }
            pq.push(b[i]);
            sum += b[i];
        }
        cout<<ans<<endl;
    }

    signed main()
    {
        fast();
        
        int t=1;
        cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

t.b.c.

posted @ 2024-10-20 16:42  Oaths  阅读(59)  评论(0)    收藏  举报