双指针题目集合

1.Codeforces Global Round 22 Problem B

题意&思路:

有n个非递减的数(a[n]),给出其前缀和的最后k项(s[ k ]),判断这样的n个数是否能存在。

原序列第n-k+1项是不确定的,但是从a[n-k+2]到a[n]可通过差分算出,通过双指针判断此区间是否为非递减。a[1]~a[n-k+1]的每一项都<=a[n-k+2],所以a[n-k+1]的最大值为(n-k+1)*a[n-k+2],只要s[0]在这个范围内即可。最后注意开long long。

#define brf(n) for(int i=0;i<n;++i)
#define brif(n) for(int i=1;i<=n;++i)
#define rtn return ;
#define IOS std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
int dr[4][2]{ {-1,0},{1,0},{0,-1},{0,1} };
#include <iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
void solve();
ll s[100010];
int main() {
	IOS;
	ll t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}
void solve() {
	ll n, k; cin >> n >> k;
	brf(k) {
		cin >> s[i];
	}
	if (k == 1) {
		cout << "yes" << endl; rtn;
	}
	ll pre = s[1] - s[0];
	for (int i = 2; i < k; i++) {
		ll cur = s[i] - s[i - 1];
		if (cur < pre) {
			cout << "no" << endl; rtn;
		}
		pre = cur;
	}
	if (s[0] <= (n - k + 1ll) * (s[1] - s[0])) {
		cout << "yes" << endl;
	}
	else {
		cout << "no" << endl;
	}
	rtn;
}

2.Codeforces Round #820 (Div. 3) Problem D

题意&思路:这个题读题就花了半小时...,但是这个题思路还是不错

有n个人要去餐厅吃饭,每个人的实际花费为xi,预算为yi。每天都只有一组人去吃饭,每组最少两个人且消费必须小于等于预算。去过的人就不再去了。问这些人最多能吃多少天饭?

要想吃饭的天数多,那么每组人数只能是两个人。求出每个人剩下的钱:预算-实际花费的值,从小到大排序。每次选最右边和最左边的人配对,若不能配对,淘汰最右边的;配对成功的也都去掉。

#define brf(n) for(int i=0;i<n;++i)
#define brif(n) for(int i=1;i<=n;++i)
#define rtn return ;
#define IOS std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
int dr[4][2]{ {-1,0},{1,0},{0,-1},{0,1} };
#include <iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int solve();
const int N=1e5+10;
int x[N], y[N];
int main() {
	IOS;
	int  t;
	cin >> t;
	while (t--) {
		cout<<solve()<<endl;
	}
	return 0;
}
int solve() {
	int ans = 0;
	int n; cin >> n;
	brf(n)cin >> x[i];
	brf(n) {
		cin >> y[i];
		y[i] -= x[i];
	}
	sort(y, y + n);
	int i = 0, j = n - 1;
	while (i < j) {
		if (y[i] + y[j]>=0) {
			i++; j--;
			ans++;
		}
		else {
			i++;
		}
	}
	return ans;
}

3.LCCUP'22  Problem C (Sliding Window)

题意&思路:给定一个一维数组,若一个区间里的每种数字的数量都不大于cnt,则符合要求。问一共有多少子区间满足要求。

感觉这个题一个关键的地方是若区间[j,i]满足条件,那么其所有子区间都满足条件,共i-j+1个子区间,所以只需要找到所有的最大区间就行了。

int beautifulBouquet(vector<int>& f, int cnt) {
        unordered_map<int,int> map;    
        int ans=0;
        for(int i=0,j=0;i<f.size();i++){
            map[f[i]]++;
            while(map[f[i]]>cnt){
                map[f[j]]--;
                j++;
            }
            ans+=i-j+1;
        }
        return ans;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lorry_521

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值