首页 > 其他分享 >CF1935 Codeforces Round 932 (Div. 2)

CF1935 Codeforces Round 932 (Div. 2)

时间:2024-03-25 17:44:06浏览次数:22  
标签:int sum Codeforces long solve ans 932 Div first

C. Messenger in MAC

给两个数组 a,b 和 一个整数L.寻找一个关于a,b下标的序列p,使得 \(\sum a_{p_i} + \sum |b_{p_i}-b_{p_{i+1}}| \leq L\)

Solution

Key1: 按照 b 从小到大排序一定是最优的.

Key2: 固定 \(b_l\) ,\(b_r\) ,那么 \(\sum^r_l |b_{p_i}-b_{p_{i+1}}| = b_r - b_l\) ,是一个固定值,就可以随便删中间的a_i 和 b_i ,只需使 \(\sum a \leq L - (b_r - b_l)\) .

只需要固定 \(l\) ,然后从左到右扩展.优先队列维护当前 a 的最大值,如果sum超出,就删去最大元素.

#include<bits/stdc++.h>

using namespace std;

#define int long long

void solve()
{
    int n,L;
    cin>>n>>L;
    vector<pair<int,int> >a(n);
    for(int i=0;i<n;i++)    
        cin>>a[i].second>>a[i].first;
    // b:first  a:second
    sort(a.begin(),a.end());
    int ans = 0;
    for(int l=0;l<n;l++){
        priority_queue<int> q;
        int sum = 0;
        for(int r =l;r<n;r++){
            q.push(a[r].second);
            sum+=a[r].second;
            while(q.size()&&sum > L -(a[r].first-a[l].first)){
                sum -= q.top();
                q.pop();
            }
            ans = max(ans,(int)q.size());
        }
    }

    cout<<ans<<'\n';
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T = 1;
    cin>>T;

    while(T--)
        solve();

    return 0;
}

\(O(n^2logn)\)

D. Exam in MAC

给一个集合 \(s\) 和正整数 \(c\) 问 \(0~c\) 中有多少个数对 \((x,y)\) , \(x\leq y\) 满足 x+y not in s,且 y-x not in s.

Solution

直接计算满足条件的 \(x,y\) 是困难的,因为判断满足条件则需要挨个条件都判断,集合 \(s\) 的大小为1e5.

但只要算出所有情况减去不合法的情况数就可以了.

所有情况数为 \(C^{2}_{n+2}\) (可以列一下情况看看)

不妨假设 \(x+y = t \in s\) ,那么就有 \(t/2+1\) 种情况.
再若 \(y-x = t \in s\),那么就有 \(c - t + 1\) 种情况.
然而考虑到上述两种情况是会有重复的:比如 \(1+2=3,2-1=1\) ,在考虑\(s_i=3\) 时过滤掉 \((1,2)\), \(s_i = 1\) 又会再去掉一次这种情况.这样就计算重复了.

第一遍做的时候思路卡在这里了,于是看了一下题解的Hint.说对于方程组 \(x+y = s_i,y-x = s_j\) ,其解只会有0或1个.
也就是说,重复情况只会多算0次或1次.

考虑上述方程组的解为 \(x = (s_i-s_j)/2\), \(y = (s_i+s_j)/2\) ,又 \(x\) 和 \(y\) 均为整数,那么只有 \(s_i\) 和 \(s_j\) 奇偶性相同时才会有整数解.
那么不妨将这些数按奇偶分成左右两组.重复计算的情况一定是从左边挑出两个或者从右边挑出两个.(为了保证0<=x<=y<=c,我们每次从集合中按照组合的方式挑出两个,而不是有放回的挑出两个,这样可以避免出现x<0或者x>y的情况.不要忘了两个数若为同一个也是可以的.所以最后答案加上集合s的所有数的个数)

并且上述方程组解出来的x和y一定是有效的(都小于等于c,显然).

那么最后答案显然为
$ans = C^{2}_{n+2} - \sum {\forall t \in S} (t/2+1) - \sum {\forall t \in S} (n - t + 1) + C^{2} + C^{2} + n $.

最后交上一发过掉,hh.(你说得对但是t宝是怎么做到4分钟读题+1A的...!!!)

#include<bits/stdc++.h>

using namespace std;

#define int long long

void solve()
{
    int n,c;
    cin>>n>>c;
    int odd = 0,even = 0;
    vector<int>a(n);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]%2){
            odd++;
        }
        else
            even++;
    }
    int res = (c+1)*(c+2)/2;
    for(int i=0;i<n;i++){
        res -= a[i]/2+1;
        res -= (c-a[i]+1);
    }
    // cout<<res<<' ';
    res += (odd*(odd-1))/2 + (even*(even-1))/2;
    res += n;
    cout<<res<<'\n';
    
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T = 1;
    cin>>T;

    while(T--)
        solve();

    return 0;
}

标签:int,sum,Codeforces,long,solve,ans,932,Div,first
From: https://www.cnblogs.com/oijueshi/p/18094447

相关文章

  • codeforces div3 935
    Problem-E-Codeforces题解:一道二分糖题首先我们先在原序列跑一遍题目给的二分,然后跑出最后的l点我们稍微思考一下,这个l这个点一定小于或者等于x为什么呢一个在这个二分里,如果最后的点是大于x的那么必定被r拿走,因为判断上l只能接收比x小的地点所以我们知道l以后,要么就是l=......
  • cfEduRound163div2--D题解
    D-TandemRepeats?题意:做法:因为字符串长度较少,可以考虑枚举。or--动态规划voidsolve(){//D枚举//枚举!!!!!!!!!!stringstr;cin>>str;intn=str.size(),ans=0;for(inti=1;i<=n/2;i++){//枚举一半!!!intcnt=0;for(intj=0;......
  • SMU Winter 2024 div2 ptlks的周报Week 6(3.18-3.24)
    不难想到,要求环的期望,只需求出所有可能的环的长度总和和不相邻点对的组数。而边数确定,则只需求环的总长。对于两个不相邻的点x,y,所形成的环的长度等于两点深度之差加一,\(\vertdp[x]-dp[y]\vert+1\),不妨令x为根节点,则只需求所有节点的深度之和,再减去相邻的点,最后对树进行换根dp,输出......
  • codeforces div_2 936 题解报告
    codeforcesdiv_2936题解报告比赛链接:https://codeforces.com/contest/1946A.MedianofanArray做法tag:签到题目翻译给定一个长度为\(n\)的数组\(a\),记数组中非降序排列中第\({\lceil\fracn2\rceil}\)个数是数组a的中位数。我们可以以下操作。选择一个数\(i\in[......
  • Codeforces Round 936 (Div. 2) D
    AB没什么好说的C二分答案,写的还是不够快,但是也很好了。D的问题有点大。我好像一直有一个不对的贪心再用,对于二进制的。就是我会觉得一堆树或起来小于一个数字,这种限制是每个数字都小于那个数字就可以了。但是实际上这就是一个很明显错误的贪心。然后另一个反映就是,对于二进......
  • Codeforces Round 936 (Div. 2)
    CodeforcesRound936(Div.2) A.MedianofanArray题意:给一串数字,每次操作可以将一个数字+1,求最少多少次操作使得数组中位数增加思路:分奇偶讨论:1:如果是奇数的话看中间的数字,如果中间的数字只出现过一次,那么次数就是1,否则看从中间位到右边最后出现这个数字的地方看这个数......
  • 如何实现一个div垂直水平居中?
    第一种:定位        第一种思路:父元素relative,子元素absolute,left为50%,top为50%,再给子div设置距左和距上是自身的一半:transform:translate(-50%,-50%)。代码实现:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="......
  • 每日一题 第二十六期 Codeforces Round 936 (Div. 2)
    A.MedianofanArraytimelimitpertest:1secondmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputYouaregivenanarrayaa......
  • 每日一题 第二十七期 Codeforces Round 936 (Div. 2)
    B.MaximumSumtimelimitpertest:1secondmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputYouhaveanarrayaaaof......
  • Codeforces Round 935 (Div. 3)
    A-SettingupCamp思路:判断c能否填充b让b为3的倍数查看代码voidsolve(){inta,b,c;cin>>a>>b>>c;intans=a+b/3;b%=3;if(b>0&&b+c<3)cout<<-1<<'\n';else{ans+=(b+c+2)/3;c......