藍橋刷題
3227
找到最多的數
方法一:摩爾投票法
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
? int n,m;
? cin>>n>>m;
? int a[m*n];
? for(int i=0;i<n*m;i++)
? {
? ? cin>>a[i];
? }
? int cand=-1,vote=0;
? for(auto num:a)
? {
? ? if(vote==0)
? ? {
? ? ? cand=num;
? ? }
? ? if(cand==num)
? ? {
? ? ? vote++;
? ? }
? ? else
? ? {
? ? ? vote--;
? ? }
?
? }
? cout<<cand;
? return 0;
}
方法二:sort()快排法
#include <bits/stdc++.h>
using namespace std;
int a[1000005];
#define int long long
signed main()
{
? int n,m;
? cin>>n>>m;
? for(int i=0;i<n*m;i++)
? {
? ? cin>>a[i];
? }
? sort(a,a+(n*m));
? int nums=a[n*m/2];
? cout<<nums;
? return 0;
}
1372.
美麗的區間
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=10000005;
int a[N];
signed main()
{
? ios::sync_with_stdio(0);
? cin.tie(0);
? int n,k;
? int ans=1000000005;
? cin>>n>>k;
? for(int i=1;i<=n;i++)
? {
? ? cin>>a[i];
? }
? int sum=0;
? for(int l=0,r=1;l<n;l++)
? {
? ?while(r<=n&&sum<k)
? ?{
? ? ?sum+=a[r];
? ? ?r++;
? ?}
? ?if(sum>=k){ans=min(ans,r-l);}
? ?sum-=a[l];
? }
? if(ans==1000000005)
? {
? ? cout<<0;
? }
? else{
cout<<ans;
? }
? return 0;
}
2695.
聰明的小羊肖恩
#include<bits/stdc++.h>
using namespace std;
#define int long long?
const int N = 2000009;
int a[N];
signed main()
{
? int n , L , R ; cin >> n >> L >> R;
? for(int i = 1; i <= n; i++) cin >> a[i];
? sort(a + 1,a + 1 + n);
? int ?res = 0;
? for(int i = 1; i <= n; i++)
? {
? ? int ?l = L - a[i] , r = R - a[i];
? ? res += upper_bound(a + i + 1,a + 1 + n,r) - lower_bound(a + i + 1,a + 1 + n,l);
? }
? cout << res << endl;
? return 0;
}