zoukankan      html  css  js  c++  java
  • HDU 6319 单调队列

    Problem A. Ascending Rating

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
    Total Submission(s): 1310    Accepted Submission(s): 347


    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+m1], and then inspect each contestant from left to right. Initially, he will write down two numbers maxrating=0 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(1T2000), denoting the number of test cases.
    In each test case, there are 7 integers n,m,k,p,q,r,MOD(1m,kn107,5p,q,r,MOD109) 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(0ai109), 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<in) are then produced as follows :
    ai=(p×ai1+q×i+r)modMOD

    It is guaranteed that n7×107 and k2×106.
     
    Output
    Since the output file may be very large, let's denote maxratingi and counti as the result of interval [i,i+m1].
    For each test case, you need to print a single line containing two integers A and B, where :
    AB==i=1nm+1(maxratingii)i=1nm+1(countii)

    Note that ``'' denotes binary XOR operation.
     
    Sample Input
    1
    10 610 5 5 5 5
    3 2 2 1 5 7 6 8 2 9
     
    Sample Output
    46 11

    解析 直接从后往前扫一遍  队列的长度就是  count

     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 #include <iostream>
     6 #include <sstream>
     7 #include <algorithm>
     8 #include <string>
     9 #include <queue>
    10 #include <map>
    11 #include <vector>
    12 using namespace std;
    13 const int maxn = 1e7+50;
    14 const int inf = 0x3f3f3f3f;
    15 const double epx = 1e-10;
    16 typedef long long ll;
    17 struct node
    18 {
    19     int x,y;   //x值 y下标
    20 } v[maxn];
    21 int a[maxn],mn[maxn],mx[maxn];
    22 int n,m;
    23 void getmax()
    24 {
    25     int head=1,tail=0;
    26     for(int i=1; i<m; i++)
    27     {
    28         while(head<=tail&&a[i]>=v[tail].x)
    29             tail--;
    30         v[++tail].x=a[i],v[tail].y=i;
    31        // cout<<head<<" "<<tail<<" "<<v[tail].x<<" "<<i<<endl;
    32     }
    33     for(int j=m; j<=n; j++)
    34     {
    35         while(head<=tail&&a[j]>=v[tail].x)
    36             tail--;
    37         v[++tail].x=a[j],v[tail].y=j;
    38         while(j-v[head].y>=m)
    39             head++;
    40         mx[j]=v[head].x;
    41         //cout<<head<<" "<<tail<<" "<<v[tail].x<<" "<<j<<endl;
    42         mn[j]=tail-head+1;
    43     }
    44 }
    45 int main()
    46 {
    47     int t,k,p,q,r,mod;
    48     scanf("%d",&t);
    49     while(t--)
    50     {
    51         scanf("%d %d %d %d %d %d %d",&n,&m,&k,&p,&q,&r,&mod);
    52         for(int i=1;i<=k;i++)
    53         {
    54             mx[i]=mn[i]=0;
    55             scanf("%d",&a[i]);
    56         }
    57         for(int i=k+1;i<=n;i++)
    58         {
    59             mx[i]=mn[i]=0;
    60             a[i]=(1ll*p*a[i-1]%mod+1ll*q*i%mod+r)%mod;
    61         }
    62         reverse(a+1,a+n+1);
    63         getmax();
    64         ll ans1=0,ans2=0;
    65         for(int i=m; i<=n; i++)
    66         {
    67             //cout<<mx[i]<<" "<<mn[i]<<" "<<n-i+1<<endl;
    68             ans1+=(int)mx[i]^(n-i+1);
    69             ans2+=(int)mn[i]^(n-i+1);
    70         }
    71         printf("%lld %lld
    ",ans1,ans2);
    72     }
    73 }
  • 相关阅读:
    贪心法解活动安排问题
    求两个等长的已排序数组的中位数(《算法导论》P113习题9.3-8)
    0-1背包问题
    动态规划法解最长公共子序列问题
    动态规划法解矩阵链乘问题
    常见的算法设计策略
    A*算法与8数字谜题(参见《算法》P226习题2.5.32)
    keepalive+redis 主从高可用
    sharding-jdbc 读写分离+水平分表
    hash一致算法原理
  • 原文地址:https://www.cnblogs.com/stranger-/p/9392937.html
Copyright © 2011-2022 走看看