hdu 4193 单调队列

题目:

Non-negative Partial Sums

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2849    Accepted Submission(s): 977


Problem Description
You are given a sequence of n numbers a 0,..., a n-1. A cyclic shift by k positions (0<=k<=n-1) results in the following sequence: a k a k+1,..., a n-1, a 0, a 1,..., a k-1. How many of the n cyclic shifts satisfy the condition that the sum of the fi rst i numbers is greater than or equal to zero for all i with 1<=i<=n?
 

Input
Each test case consists of two lines. The fi rst contains the number n (1<=n<=10 6), the number of integers in the sequence. The second contains n integers a 0,..., a n-1(-1000<=a i<=1000) representing the sequence of numbers. The input will finish with a line containing 0.
 

Output
For each test case, print one line with the number of cyclic shifts of the given sequence which satisfy the condition stated above.
 

Sample Input
  
  
3 2 2 1 3 -1 1 1 1 -1 0
 

Sample Output
  
  
3 2 0
 

Source

给你一个n项的序列,每次可以把序列的首项移动到末尾,显然一共可以构成 n 种序列,问一共有多少种序列满足条件:序列的前 i 项和都大于等于0(i:1~n)。


分析:

环的问题可以通过开双倍数组并将输入数据复制两份接起来来解决

[i,j]的每一个前缀和都大于0等价于从数列起点开始的前缀和在[I,J]部分的最小值都满足比sum(i-1)大

问题转化为高效求[i,j]区间的最小值 即固定区间求最值 联想到单调队列,相当于一个定长的滑块往后滑,滑的过程中通过入队出队操作使得队首值即为最小值


代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1000010<<1;

int sum[maxn],q[maxn];
int n,cnt,Front,rear;
//队列中维护[1,2n]的下标 使得这些下标对应的前缀和递增 这样队首元素对应的前缀和是最小的
inline void in(int i) {
    while(Front<=rear&&sum[q[rear]]>sum[i]) rear--;//保持前缀和严格递增  或者不降也行
    q[++rear]=i;//插到队尾
}

inline void out(int i,int n) {//删除i使得队列大小为n
    if(q[Front]<=i-n) Front++;//i-n属于[0,n) 从队首移除
}

int main(){//1528MS	9548K
    while (scanf("%d",&n)==1 && n) {
        sum[0]=0;
        cnt=0;
        Front=0;
        rear=-1;
        for(int i=1;i<=n;++i) {
            scanf("%d",&sum[i]);
            sum[i+n]=sum[i];
        }
        for (int i=2;i<n+n;++i) sum[i]+=sum[i-1];
        for(int i=1;i<n;++i) in(i);
        //现在队列里面有n-1个元素 后面每进一个就要出一个
        for(int i=n;i<n+n;++i){
            in(i);
            out(i,n);
            if(sum[q[Front]]-sum[i-n]>=0) cnt++;
        }
        printf("%d\n",cnt);
    }
    return 0;
}


对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值