【HDOJ】2430 Beans

这题目用线段树超时了,其实也差不多应该超时。10^6大数据量。看了一下网上的解法是单调队列。大概了解了一下,是个挺有意思的数据结构。
首先,需要求满足0<=(S[r]-S[l])%p<=k时,(S[r]-S[l])的最大值。
由于S[r]>=S[l],因此即求S[r]%p-k <= S[l]%p <= S[r]%p的最优解。
单调队列可解,按S[i]%p和i排列,每次固定r,求最优的l值,l在队头。

  1 /* 2430 */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <algorithm>
 12 #include <cstdio>
 13 #include <cmath>
 14 #include <ctime>
 15 #include <cstring>
 16 #include <climits>
 17 #include <cctype>
 18 #include <cassert>
 19 #include <functional>
 20 #include <iterator>
 21 #include <iomanip>
 22 using namespace std;
 23 //#pragma comment(linker,"/STACK:102400000,1024000")
 24 
 25 #define sti                set<int>
 26 #define stpii            set<pair<int, int> >
 27 #define mpii            map<int,int>
 28 #define vi                vector<int>
 29 #define pii                pair<int,int>
 30 #define vpii            vector<pair<int,int> >
 31 #define rep(i, a, n)     for (int i=a;i<n;++i)
 32 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 33 #define clr                clear
 34 #define pb                 push_back
 35 #define mp                 make_pair
 36 #define fir                first
 37 #define sec                second
 38 #define all(x)             (x).begin(),(x).end()
 39 #define SZ(x)             ((int)(x).size())
 40 #define lson            l, mid, rt<<1
 41 #define rson            mid+1, r, rt<<1|1
 42 
 43 typedef struct node_t {
 44     int q, p;
 45     
 46     node_t() {}
 47     node_t(int q, int p):
 48         q(q), p(p) {}
 49     
 50     friend bool operator< (const node_t& a, const node_t& b) {
 51         if (a.q == b.q)
 52             return a.p < b.p;
 53         return a.q < b.q;
 54     }
 55         
 56 } node_t;
 57 
 58 const int maxn = 1e6+5;
 59 int a[maxn];
 60 __int64 sum[maxn];
 61 node_t nd[maxn];
 62 int n, p, k;
 63 int Q[maxn];
 64 __int64 ans;
 65 
 66 void solve() {
 67     int l = 1, r = 0;
 68     __int64 tmp;
 69     ans = -1;
 70     
 71     rep(i, 0, n+1) {
 72         while (l<=r && nd[i].p < nd[Q[r]].p)
 73             --r;
 74         Q[++r] = i;
 75         while (l<=r && (nd[i].q - nd[Q[l]].q) > k)
 76             ++l;
 77         if (l >= r)
 78             continue;
 79         tmp = sum[nd[i].p] - sum[nd[Q[l]].p];
 80         ans = max(ans, tmp/p);
 81     }
 82 }
 83 
 84 int main() {
 85     ios::sync_with_stdio(false);
 86     #ifndef ONLINE_JUDGE
 87         freopen("data.in", "r", stdin);
 88         freopen("data.out", "w", stdout);
 89     #endif
 90     
 91     int t;
 92     
 93     scanf("%d", &t);
 94     rep(tt, 1, t+1) {
 95         scanf("%d %d %d", &n, &p, &k);
 96         nd[0].p = nd[0].q = 0;
 97         sum[0] = 0;
 98         rep(i, 1, n+1) {
 99             scanf("%d", &a[i]);
100             sum[i] = sum[i-1] + a[i];
101             nd[i].p = i;
102             nd[i].q = sum[i] % p;
103         }
104         sort(nd, nd+n+1);
105         solve();
106         printf("Case %d: %I64d\n", tt, ans);
107     }
108     
109     #ifndef ONLINE_JUDGE
110         printf("time = %d.\n", (int)clock());
111     #endif
112     
113     return 0;
114 }

 

posted on 2015-12-26 20:33  Bombe  阅读(139)  评论(0编辑  收藏  举报

导航