hdu 5289 ST表+双指针或者优先队列或者multiset

12 篇文章 0 订阅
9 篇文章 0 订阅

Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to some staffs who were in the same group. In a group, the difference of the ability of any two staff is less than k, and their numbers are continuous. Tom want to know the number of groups like this.
Input
In the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers n, k (1<=n<=100000, 0 k<=10^9),indicate the company has n persons, k means the maximum difference between abilities of staff in a group is less than k. The second line contains n integers:a1,a2,…,an(0<=ai<=10^9),indicate the i-th staff’s ability.
Output
For each test,output the number of groups.
Sample Input
2
4 2
3 1 2 4
10 5
0 3 4 5 2 1 6 7 8 9
Sample Output
5
28

Hint
First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]

题意:问数组a中,区间[i,j]中最大值和最小值之差不超过k,问这样的区间有多少个。
好几种写法:原理一样 找到区间的最小最大值,因为是连续的,所以把前面不符合的情况去掉就好。

解法1:st+贪心+双指针。

#include <bits/stdc++.h>
using namespace std;
int a[200005];
int mx[200005][30];
int mn[200005][30];
int n;
int num;
int k;
void init()
{
  for(int i=1;i<=n;i++)
    mn[i][0]=mx[i][0]=a[i];
    for(int j = 1; (1<<j) <= n; ++j)  
        for(int i = 1; i + (1<<j) - 1 <= n; ++i)  
        {  
            mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);  
            mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);  
        }  
}

int rmq(int i,int j)
{
     int k = 0;  
    while((1 << (k + 1)) <= j - i + 1) k++;  
    int maxx=max(mx[i][k],mx[j-(1<<k)+1][k]);
    int minn=min(mn[i][k],mn[j-(1<<k)+1][k]);
    return maxx-minn;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {

        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        init();
        int h=1;
        long long sum=0;
        for(int i=1;i<=n;i++)
        {
            while(rmq(h,i)>=k&&h<i) h++;//把不行的排除掉 剩下的可以随意搭配
            sum+=(i-h+1);//以i结束的组的数量 例如 i=5,h=3,就是3,(5~5),(5~4),(5~3).
        }
        printf("%lld\n",sum );
    }
}

解法2:multiset 维护,因为multiset 自动维护区间最大最小值,所以只有双指针不断排除就好,但是没有st好,st更快。。

#include <bits/stdc++.h>
using namespace std;

multiset<int> minn;
multiset<int,greater<int> > maxn;
int a[100005];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,k;
        scanf("%d%d",&n,&k);
        maxn.clear();
        minn.clear();
        long long ans=0;
        int j=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            maxn.insert(a[i]);
            minn.insert(a[i]);
            multiset<int>::iterator it1,it2;
            while(j<=i&&*maxn.begin()-*minn.begin()>=k)
            {//j指针一直删除数据,直到后面的再次满足。相当于实现了线段树的删除操作
                it1=maxn.find(a[j]);
                it2=minn.find(a[j]);
                maxn.erase(it1);
                minn.erase(it2);
                j++;
            }
            ans+=maxn.size();
        }
        printf("%lld\n",ans );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值