HDU6319 Problem A. Ascending Rating(2018HDU多校联赛第三场,单调队列)

Problem Description

Before the start of contest, there are n ICPC contestants waiting in a long queue. They are labeled by 1 to n from left to right. It can be easily found that the i-th contestant’s QodeForces rating is ai.
Little Q, the coach of Quailty Normal University, is bored to just watch them waiting in the queue. He starts to compare the rating of the contestants. He will pick a continous interval with length m, say [l,l+m−1], and then inspect each contestant from left to right. Initially, he will write down two numbers maxrating=−1 and count=0. Everytime he meets a contestant k with strictly higher rating than maxrating, he will change maxrating to ak and count to count+1.
Little T is also a coach waiting for the contest. He knows Little Q is not good at counting, so he is wondering what are the correct final value of maxrating and count. Please write a program to figure out the answer.

Input

The first line of the input contains an integer T(1≤T≤2000), denoting the number of test cases.
In each test case, there are 7 integers n,m,k,p,q,r,MOD(1≤m,k≤n≤107,5≤p,q,r,MOD≤109) in the first line, denoting the number of contestants, the length of interval, and the parameters k,p,q,r,MOD.
In the next line, there are k integers a1,a2,…,ak(0≤ai≤109), denoting the rating of the first k contestants.
To reduce the large input, we will use the following generator. The numbers p,q,r and MOD are given initially. The values ai(k

Output

Since the output file may be very large, let’s denote maxratingi and counti as the result of interval [i,i+m−1].
For each test case, you need to print a single line containing two integers A and B, where :
A = i = 1 n m + 1 ( m a x r a t i n g i i ) B = i = 1 n m + 1 ( c o u n t i i )

Note that “⊕” denotes binary XOR operation.

Sample Input

1
10 6 10 5 5 5 5
3 2 2 1 5 7 6 8 2 9

Sample Output

46 11

思路

给定一个序列 a[1..n],对于每个长度为 m 的连续子区间, 求出区间 a 的最大值以及从左往右扫描该区间时 a 的最大值的变化次数。

题目给出了从1k的序列,剩下的序列可以由公式推出来。

然后用单调队列正着扫一遍维护一下每一个小区间的最大值,并且记录结果。

然后又用单调队列倒着扫一遍,也是维护最大值,每扫过一个区间,区间内最大值的变化次数就是这个单调队列当前的元素数量

卡时间,要用数组模拟,用dequeTLE

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef long long ll;
typedef pair<ll, ll> pir;
const ll N = 1e7 + 10;
ll a[N];
ll read()
{
    char ch=getchar();
    ll x=0;
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x;
}
int Q[N],y[N];
int main()
{
    ll t, n, m, k, p, q, r, mod;
    t=read();
    while (t--)
    {
        n=read();
        m=read();
        k=read();
        p=read();
        q=read();
        r=read();
        mod=read();
        for (ll i = 1; i <= k; i++)
            a[i]=read();
        for (ll i = k + 1; i <= n; i++)
            a[i] = (p * a[i - 1] + q * i + r)%mod ;
        ll l=1,r=0,sum1=0,sum2=0;
        for (ll i = 1; i <= n; i++)
        {
            while(l<=r&&Q[r]<=a[i]) r--;
            Q[++r]=a[i];
            y[r]=i;
            if(y[l]<=i-m)l++;
            if(i>=m) sum1+=Q[l]^(i-m+1);
        }
        l=1,r=0;
        for(ll i=n; i>=1; i--)
        {
            while(l<=r&&Q[r]<=a[i])r--;
            Q[++r]=a[i];
            y[r]=i;
            if(y[l]>=i+m)l++;
            if(i<=n-m+1)
                sum2+=(r-l+1)^i;
        }
        printf("%lld %lld\n",sum1,sum2);
    }
    return 0;
}

没有过的deque代码:

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef long long ll;
typedef pair<ll, ll> pir;
const ll N = 1e7 + 10;
ll a[N];
deque<pir> q1;
deque<pir> q2; 
ll read()
{
    char ch = getchar();
    ll x = 0;
    while (ch < '0' || ch > '9')
        ch = getchar();
    while (ch >= '0' && ch <= '9')
        x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x;
}
int main()
{
    ll t, n, m, k, p, q, r, mod;
    //scanf("%lld", &t);
    t = read();
    while (t--)
    {
        while (!q1.empty())
            q1.pop_back();
        // scanf("%lld%lld%lld%lld%lld%lld%lld", &n, &m, &k, &p, &q, &r, &mod);
        n = read();
        m = read();
        k = read();
        p = read();
        q = read();
        r = read();
        mod = read();
        for (ll i = 1; i <= k; i++)
            //scanf("%lld", &a[i]);
            a[i] = read();
        for (ll i = k + 1; i <= n; i++)
        {
            a[i] = (p * a[i - 1] + q * i + r);
            if (a[i] > mod)
                a[i] -= mod;
        }
        ll sum1 = 0, sum2 = 0;
        for (ll i = 1; i <= n; i++)
        {
            while (!q1.empty() && q1.back().first <= a[i])
                q1.pop_back();
            q1.push_back(make_pair(a[i], i));
            if (i >= m)
            {
                while (!q1.empty() && q1.front().second <= i - m)
                    q1.pop_front();
                sum1 += (i - m + 1) ^ q1.front().first;
            }
        }
        while (!q2.empty())
            q2.pop_back();
        for (ll i = n, j = 1; i >= 1; i--, j++)
        {
            while (!q2.empty() && q2.back().first <= a[i])
                q2.pop_back();
            q2.push_back(make_pair(a[i], j));
            if (j >= m)
            {
                while (!q2.empty() && q2.front().second <= j - m)
                    q2.pop_front();
                sum2 += i ^ q2.size();
            }
        }
        printf("%lld %lld\n", sum1, sum2);
    }
    return 0;
}

Problem Description

Before the start of contest, there are n ICPC contestants waiting in a long queue. They are labeled by 1 to n from left to right. It can be easily found that the i-th contestant’s QodeForces rating is ai.
Little Q, the coach of Quailty Normal University, is bored to just watch them waiting in the queue. He starts to compare the rating of the contestants. He will pick a continous interval with length m, say [l,l+m−1], and then inspect each contestant from left to right. Initially, he will write down two numbers maxrating=−1 and count=0. Everytime he meets a contestant k with strictly higher rating than maxrating, he will change maxrating to ak and count to count+1.
Little T is also a coach waiting for the contest. He knows Little Q is not good at counting, so he is wondering what are the correct final value of maxrating and count. Please write a program to figure out the answer.

Input

The first line of the input contains an integer T(1≤T≤2000), denoting the number of test cases.
In each test case, there are 7 integers n,m,k,p,q,r,MOD(1≤m,k≤n≤107,5≤p,q,r,MOD≤109) in the first line, denoting the number of contestants, the length of interval, and the parameters k,p,q,r,MOD.
In the next line, there are k integers a1,a2,…,ak(0≤ai≤109), denoting the rating of the first k contestants.
To reduce the large input, we will use the following generator. The numbers p,q,r and MOD are given initially. The values ai(k

Output

Since the output file may be very large, let’s denote maxratingi and counti as the result of interval [i,i+m−1].
For each test case, you need to print a single line containing two integers A and B, where :
A = i = 1 n m + 1 ( m a x r a t i n g i i ) B = i = 1 n m + 1 ( c o u n t i i )

Note that “⊕” denotes binary XOR operation.

Sample Input

1
10 6 10 5 5 5 5
3 2 2 1 5 7 6 8 2 9

Sample Output

46 11

思路

给定一个序列 a[1..n],对于每个长度为 m 的连续子区间, 求出区间 a 的最大值以及从左往右扫描该区间时 a 的最大值的变化次数。

题目给出了从1k的序列,剩下的序列可以由公式推出来。

然后用单调队列正着扫一遍维护一下每一个小区间的最大值,并且记录结果。

然后又用单调队列倒着扫一遍,也是维护最大值,每扫过一个区间,区间内最大值的变化次数就是这个单调队列当前的元素数量

卡时间,要用数组模拟,用dequeTLE

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef long long ll;
typedef pair<ll, ll> pir;
const ll N = 1e7 + 10;
ll a[N];
ll read()
{
    char ch=getchar();
    ll x=0;
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x;
}
int Q[N],y[N];
int main()
{
    ll t, n, m, k, p, q, r, mod;
    t=read();
    while (t--)
    {
        n=read();
        m=read();
        k=read();
        p=read();
        q=read();
        r=read();
        mod=read();
        for (ll i = 1; i <= k; i++)
            a[i]=read();
        for (ll i = k + 1; i <= n; i++)
            a[i] = (p * a[i - 1] + q * i + r)%mod ;
        ll l=1,r=0,sum1=0,sum2=0;
        for (ll i = 1; i <= n; i++)
        {
            while(l<=r&&Q[r]<=a[i]) r--;
            Q[++r]=a[i];
            y[r]=i;
            if(y[l]<=i-m)l++;
            if(i>=m) sum1+=Q[l]^(i-m+1);
        }
        l=1,r=0;
        for(ll i=n; i>=1; i--)
        {
            while(l<=r&&Q[r]<=a[i])r--;
            Q[++r]=a[i];
            y[r]=i;
            if(y[l]>=i+m)l++;
            if(i<=n-m+1)
                sum2+=(r-l+1)^i;
        }
        printf("%lld %lld\n",sum1,sum2);
    }
    return 0;
}

没有过的deque代码:

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef long long ll;
typedef pair<ll, ll> pir;
const ll N = 1e7 + 10;
ll a[N];
deque<pir> q1;
deque<pir> q2; 
ll read()
{
    char ch = getchar();
    ll x = 0;
    while (ch < '0' || ch > '9')
        ch = getchar();
    while (ch >= '0' && ch <= '9')
        x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x;
}
int main()
{
    ll t, n, m, k, p, q, r, mod;
    //scanf("%lld", &t);
    t = read();
    while (t--)
    {
        while (!q1.empty())
            q1.pop_back();
        // scanf("%lld%lld%lld%lld%lld%lld%lld", &n, &m, &k, &p, &q, &r, &mod);
        n = read();
        m = read();
        k = read();
        p = read();
        q = read();
        r = read();
        mod = read();
        for (ll i = 1; i <= k; i++)
            //scanf("%lld", &a[i]);
            a[i] = read();
        for (ll i = k + 1; i <= n; i++)
        {
            a[i] = (p * a[i - 1] + q * i + r);
            if (a[i] > mod)
                a[i] -= mod;
        }
        ll sum1 = 0, sum2 = 0;
        for (ll i = 1; i <= n; i++)
        {
            while (!q1.empty() && q1.back().first <= a[i])
                q1.pop_back();
            q1.push_back(make_pair(a[i], i));
            if (i >= m)
            {
                while (!q1.empty() && q1.front().second <= i - m)
                    q1.pop_front();
                sum1 += (i - m + 1) ^ q1.front().first;
            }
        }
        while (!q2.empty())
            q2.pop_back();
        for (ll i = n, j = 1; i >= 1; i--, j++)
        {
            while (!q2.empty() && q2.back().first <= a[i])
                q2.pop_back();
            q2.push_back(make_pair(a[i], j));
            if (j >= m)
            {
                while (!q2.empty() && q2.front().second <= j - m)
                    q2.pop_front();
                sum2 += i ^ q2.size();
            }
        }
        printf("%lld %lld\n", sum1, sum2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/riba2534/article/details/81316250
今日推荐