The 2018 ACM-ICPC Chinese Collegiate Programming Contest Maximum Element In A Stack

 1 //利用二维数组模拟
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <utility>
 8 #include <vector>
 9 #include <map>
10 #include <queue>
11 #include <stack>
12 #include <cstdlib>
13 typedef long long ll;
14 #define lowbit(x) (x&(-x))
15 #define ls l,m,rt<<1
16 #define rs m+1,r,rt<<1|1
17 using namespace std;
18 int t,n,m,p,q;
19 const int N=5e6+9;
20 ll  a[N][2];//数据可能超int
21 unsigned int SA,SB,SC;
22 unsigned int rng61(){
23     SA^=SA <<16;
24     SA^=SA >>5;
25     SA^=SA <<1;
26     unsigned int  t=SA;
27     SA=SB;
28     SB=SC;
29     SC ^=t^SA;
30     return SC;
31 }
32 int  main()
33 {
34     scanf("%d",&t);
35     for(int i=1;i<=t;i++){
36         memset(a,0,sizeof(a));
37         scanf("%d%d%d%d%u%u%u",&n,&p,&q,&m,&SA,&SB,&SC);
38         //每次调用rng61()上面的数会变
39         int top=1;//因为取最大值时要和a[top-1][1]比较
40         //故top初始化为1
41         ll ans=0;
42         for(int i=1;i<=n;i++){
43             if(rng61()%(p+q)<p){
44                 a[top][0]=rng61()%m+1;//当前应该压入的值
45                 a[top][1]=max(a[top-1][1],a[top][0]);//令栈顶元素为当前栈内最大值
46                 top++;
47             }
48             else top--;//弹出,而弹出后的栈顶元素仍是弹出后的最大值
49             if(top<=1) ans^=0,top=1;//空时不操作
50             else {
51                 ans^=(i*a[top-1][1]);//top始终表示栈内元素个数+1
52             }
53         }
54         printf("Case #%d: %lld\n",i,ans);
55     }
56     return 0;
57 }

 

posted on 2018-07-18 09:26  cltt  阅读(111)  评论(0编辑  收藏  举报

导航