A. Stone Game
分析:
考虑从两边删,还是从一遍删,选比较小的那个
#include <iostream>
#include <algorithm>
using namespace std;
int a[200];
int main(){
int t; cin >> t;
a[101] = 101;
while(t--)
{
int n; cin >> n;
int minx = 101, maxx = 0;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
if(a[minx] > a[i]) minx = i;
if(a[maxx] < a[i]) maxx = i;
}
if(minx > maxx) swap(minx, maxx);
int ans = min(maxx, n-minx+1);
ans = min(minx + n-maxx+1, ans);
cout << ans << endl;
}
// system("pause");
}
B. Friends and Candies
分析:
把大于均值的人挑出来即可
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 200100;
typedef long long LL;
int a[N];
int main()
{
int t; cin >> t;
while(t--)
{
int n; cin >> n;
LL sum = 0;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
sum += a[i];
}
if(sum%n != 0)
{
cout << "-1" << endl;
continue;
}
int avg = sum/n;
int cnt = 0;
for(int i = 1; i <= n; i++)
{
if(a[i] > avg) cnt++;
}
cout << cnt << endl;
}
// system("pause");
}
C. Number of Pairs
分析:
先排序,排序后枚举有序对的第一个数字, 然后二分出第二个数字的合法区间.
复杂度 O ( n log n ) O(n\log n) O(nlogn)
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int N= 200100;
int a[N];
int n, l, r;
int work(int x)
{
int tl = l-a[x], tr = r-a[x];
auto p =