Codeforces Round #719 (Div. 3) D-F2

Problem - D - Codeforces

        (1)题目大意

        给你一个序列,让你求其中a[j] - a[i] = j - i的数对对数。

         (2)解题思路

                前面的对于后面用贡献,可以知道j-i是可以维护出来的,我们可以把所有值都相对于第n个来看,这样我们的j-i就相当于固定了,因此枚举i的时候,他的本身的值就可以化成a[i] - n + ps(ps是递减的步数,相对于最后一个位置n来说的),因此我们只需要用map枚举一下即可。

        (3)代码实现

#include "bits/stdc++.h"
using namespace std;
const int N = 2e5 + 10;
int a[N];
void solve()
{
    int n;
    cin >> n;
    map <int,int> mp;
    long long ans = 0,ps = 1;
    for(int i = 1;i <= n;i++) {
        cin >> a[i];
        int cnt = mp[a[i] - n + ps];
        if(cnt) ans += cnt;
        mp[a[i] - n + ps] ++;
        ps --;
    }
    cout << ans << endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T;
    cin >> T;
    while(T --) {
        solve();
    }
    return 0;
}

Problem - E - Codeforces

        (1)题目大意

                给你一行字符串,*代表羊,'.'代表草地,让你把所有的羊连接到一起,每次只能移动一只羊,求最后连到一起的最小价值是多少。

         (2)解题思路

                对于每一个羊的位置,我们可以把它记录下来,然后都向中间的羊靠拢,有点像货舱选址问题,我们只需要计算左边的羊移动到靠中间的位置的价值+右边羊移动到靠中间的位置即可。

        (3)代码实现

#include "bits/stdc++.h"
using namespace std;
const int N = 1e5 + 10;
void solve()
{
    int n;
    string s;
    cin >> n >> s;
    s = " " + s;
    vector <int> loc;
    for(int i = 1;i <= n;i++) {
        if(s[i] == '*')
            loc.push_back(i);
    }
    if(loc.size() == 0 || loc.size() == 1) {
        cout << 0 << endl;
        return ;
    }
    int idx = loc.size() / 2;
    int left = loc[idx] - 1,right = loc[idx] + 1;
    int p1 = loc[idx] - 1,p2 = loc[idx] + 1;
    long long ans = 0;
    while(p1 >= 1) {
        if(s[p1] == '*') {
            ans += left - p1;
            left --;
        }
        p1 --;
    }
    while(p2 <= n) {
        if(s[p2] == '*') {
            ans += p2 - right;
            right ++; 
        }
        p2 ++;
    }
    cout << ans << endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T;
    cin >> T;
    while(T --) {
        solve();
    }
    return 0;
}

Problem - F2 - Codeforces

        (1)题目大意

                给你一个n,t,t代表有t次交互询问,n代表数组长度为n,每个询问给你一个k,让你输出第k个零的位置是哪个,注意如果前面已经被询问出来的0的位置都会变成1。

         (2)解题思路

                如果我们只是单纯的询问判断要往哪边,那么我们的询问次数将会超过6*10^4,因此我们需要记录有用的信息,比如说某个位置已经问过了,那么我们可以用数组给他记录下来,而对于已经被问出来的位置,我们可以利用树状数组维护那个位置,给那个位置的值+1,下次询问的时候可以直接通过已经记录的加上树状数组维护的处理那一次询问,因此可以做出来了。

        (3)代码实现

#include "bits/stdc++.h"
#define PII pair<int,int>
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 10;
int s[N];
int n,t,k;
vector <int> pos(N + 1,-1);
int lowbit(int x)
{
    return x & -x;
}
void add(int p,int v)
{
    while(p <= n) {
        s[p] += v;
        p += lowbit(p);
    }
}
int qry(int p)
{
    int res = 0;
    while(p >= 1) {
        res += s[p];
        p -= lowbit(p);
    }
    return res;
}
int ask(int l,int r)
{
    if(pos[r] != -1) return pos[r] + qry(r);
    int x;
    cout << "? " << l << ' ' << r << endl;
    cin >> x;
    pos[r] = x - qry(r);
    return x;
}
void solve()
{
    cin >> n >> t;
    int l = 1,r = n;
    for(int i = 1;i <= t;i++) {
        cin >> k;
        l = 1,r = n;
        while(l <= r) {
            int mid = (l + r) >> 1;
            int cnt = ask(1,mid);
            if(mid - cnt >= k) r = mid - 1;
            else l = mid + 1;
        }
        add(l,1);
        cout << "! " << l << endl;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T;
    T = 1;
    while(T --) {
        solve();
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值