1.聪明的小羊肖恩 - 蓝桥云课 (lanqiao.cn)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod=100000007;
const int N=200010;
int n,L,R;
int a[N];
LL calc(int v){//计算数组a中两个数之和小于等于v的数对数量int l=1,r=n;LL ans=0;while(l<r){while(l<r&&a[l]+a[r]>v){r--;//不符合条件需要减小值,}//符合条件则通过r-l计算ans +=r-l;//[l,r]这个区间内都可以,l与[l+1,r]中有r-l种组合l++;}return ans;
}
void solve(){cin>>n>>L>>R;for(int i=1;i<=n;i++)cin>>a[i];sort(a+1,a+n+1);cout<<calc(R)-calc(L-1)<<'\n';
}
int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);solve();return 0;
}
1.神奇的数组 - 蓝桥云课 (lanqiao.cn)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int n, res;
int s[N], a[N], c[N];
inline void solve()
{cin >> n;for (int i = 1; i <= n; i ++ ) cin >> a[i], s[i] = s[i - 1] + a[i];for (int i = 1; i <= n; i ++ ) c[i] = c[i - 1] ^ a[i];//异或前缀和for (int i = 1,j=0; i <= n; i ++ ){//双指针模板while (((s[j] - s[i - 1]) == (c[j] ^ c[i - 1])) && j <= n) j ++;res += (j - i);//是个数而不是长度,最后一个位置是不符合条件的}cout << res << '\n';
}
signed main(){cin.tie(nullptr) -> sync_with_stdio(0);solve();return 0;
}
1.可凑成的最大花束数 - 蓝桥云课 (lanqiao.cn)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int a[N];int n,k;
int check(int mid){//花束个数int res=0;//有效花束总数for(int i=1;i<=n;i++){res+=min(mid,a[i]);//每个花的贡献值}//res表示的是所有花的数量 然后 /mid得到的是一束花中的花朵数return res/mid;//一束中花数
}
void solve()
{cin>>n>>k;for(int i=1;i<=n;i++) cin>>a[i];int l=0,r=2e14+1;while(l<r){int mid=(l+r+1)/2;if(check(mid)>=k) l=mid;//如果一束中的花数大于k,就可以包裹成更多的花束else r=mid-1;//找最多的向右边找}cout<<l;
}
signed main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);solve();return 0;
}
1.最大通过数 - 蓝桥云课 (lanqiao.cn)
#include <bits/stdc++.h>
using namespace std;
int n,m,k;
const int N=2e5+2;
typedef long long ll;
ll a[N]={0},b[N]={0};
bool check(ll mid){//输入通过的关卡数之和
//枚举每一种情况 然后取最小值 找到通过这么多关卡所需要的最小水晶数ll t=mid;ll sum=0;ll mn=1e14;while(t--){sum=0;if(t>n)continue;//防止越界if(mid-t>m)break;//防止越界sum+=a[t]+b[mid-t];mn=1ll*min(mn,sum);}return mn<=k;//下能通过的关卡数更多
}
int main()
{cin>>n>>m>>k;//a[i]表示通过前i个关卡需要的水晶数量是a[i]for(int i=1;i<=n;i++){cin>>a[i];a[i]+=a[i-1];}for(int i=1;i<=m;i++){cin>>b[i];b[i]+=b[i-1];}//前缀和ll l=0,r=n+m+2;while(l<r){ll mid=(l+r+1)/2;//右-需要还if(check(mid))l=mid;else r=mid-1;//右-}cout<<r<<'\n';return 0;
}
1.体育健将 - 蓝桥云课 (lanqiao.cn)
// 解题思路: 枚举小蓝参加的最后一个项目item[i],则剩余时间为temp - item[i].et,
// 然后我们按总时间(参赛+休息)排序,并求出其前缀和数组s,s[i]的含义是前i件项目
// 的时间和,准备好这些之后,二分答案。check函数我们将枚举的答案x带入s数组中,求出
// 前x件项目时间之和,随后与剩余时间time比较,这里有个细节是,如果前x件项目中包含
// 枚举的i,我们需要将其删去并将s[x]改为s[x+1]。
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
int n,k,ans;
long long s[N];
struct node {int et,br;
}item[N];
bool cmp(const node &a, const node &b) {return a.et + a.br <= b.et + b.br;
}
bool check(int x, int y,int time) {if(x < y) { if(s[x] <= time) return true;return false;}else {if(x + 1 > n) return false;//超出了if(s[x + 1] - item[y].br <= time) return true;//还是比总时间更小的else return false;//减去这个比赛}
}
int main() {ios::sync_with_stdio(false),cin.tie(0);cin>>n>>k;for(int i = 1; i <= n; i ++) cin>>item[i].et;for(int i = 1; i <= n; i ++) cin>>item[i].br;sort(item+1,item+1+n,cmp);//sort可以保证选的都是用的时间少的 //所以前缀和数组才右意义for(int i = 1; i <= n; i ++) s[i] = s[i-1] + item[i].et + item[i].br;for(int i = 1; i <= n; i ++) {//枚举第i个比赛最后参加int temp = k;if(temp < item[i].et) continue;//总时间小于这个比赛的参加时间 肯定不行temp -= item[i].et;//最后参加这个比赛int l = 0, r = n;while(l < r) {int mid = (l + r + 1) >> 1;//参加mid个比赛if(check(mid,i,temp)) l = mid;else r = mid-1;}ans = max(r+1,ans);//加上一个1}cout<<ans;return 0;
}