Codeforces Round #725 (Div. 3)
A - Stone Game
传送门
思路:思维
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 500;
int a[N];
int main ()
{
int t;
cin >> t;
while (t --)
{
int n, maxx = 0, minn = 0x3f3f3f3f, maxi, mini;
cin >> n;
for (int i = 1; i <= n; i ++)
{
cin >> a[i];
if (a[i] <= minn)
{
minn = a[i];
mini = i;
}
if (a[i] >= maxx)
{
maxx = a[i];
maxi = i;
}
}
int m1 = min (mini, maxi), m2 = max (mini, maxi);
cout << min (n - m2 + 1 + m1, m2 - m1 + min (n - m2 + 1, m1)) << endl;
}
}
B - Friends and Candies
传送门
思路:找到平均数,输出比平均数大的数的个数
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int main ()
{
int t;
cin >> t;
while (t --)
{
int n;
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i ++)
{
cin >> a[i];
sum += a[i];
}
if (sum % n != 0)
cout << -1 << endl;
else
{
sum /= n;
int ans = 0;
for (int i = 1; i <= n; i ++)
{
if (a[i] > sum)
ans ++;
}
cout << ans << endl;
}
}
}
C - Number of Pairs
传送门
思路:二分找答案
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 100;
int a[N];
int main ()
{
int t;
cin >> t;
while (t --)
{
long long n, L, R;
cin >> n >> L >> R;
for (int i = 0; i < n; i ++)
{
scanf ("%d", &a[i]);
}
sort (a, a + n);
long long ans = 0;
for (int i = 0; i < n; i ++)
{
long long cnt1, cnt2;
cnt1 = lower_bound (a, a + n, L - a[i]) - a;
if (cnt1 <= i + 1)
cnt1 = i + 1;
cnt2 = upper_bound (a, a + n, R - a[i]) - a;
if (cnt2 <= i + 1)
cnt2 = i + 1;
ans += cnt2 - cnt1;
if (cnt2 < n && a[cnt2] == R - a[i])
ans ++;
}
cout << ans << endl;
}
}
D - Another Problem About Dividing Numbers
传送门
思路:累除
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 2e5 + 100;
int division (int a, int res)
{
if (a == 1)
return res;
for (int i = 2; i <= sqrt (a); i ++)
{
if (a % i == 0)
{
return division (a / i, res + 1);
}
}
return res + 1;
}
int main ()
{
int t;
cin >> t;
while (t --)
{
int a, b, k;
scanf ("%d%d%d", &a, &b, &k);
if (a < b) swap (a, b);
if (k == 1 && a == b)
{
printf ("NO\n");
continue;
}
else if (k == 1 && a % b == 0)
{
printf ("YES\n");
continue;
}
else if (k == 1 && a != b && a % b != 0)
{
printf ("NO\n");
continue;
}
else if (a == 1 && b == 1 && k == 2)
{
printf ("NO\n");
continue;
}
else
{
int cnt = division (a, 0) + division (b, 0);
if (cnt < k)
printf ("NO\n");
else
printf ("YES\n");
}
}
}
E - Funny Substrings
思路:模拟+字符串处理
G - Gift Set
传送门
思路:二分找答案最大值
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int main ()
{
int t;
cin >> t;
while (t --)
{
ll x, y, a, b;
cin >> x >> y >> a >> b;
if (a > b) swap (a, b);
ll l = 0, r = 1e9, res = 0;
while (l <= r)
{
ll mid = l + r >> 1;
ll xx = x - mid * a;
ll yy = y - mid * b;
if (xx < 0 || yy < 0)
{
r = mid - 1;
continue;
}
xx /= b;
yy /= a;
if (yy >= xx)
{
res = max (res, xx + mid);
l = mid + 1;
}
else
{
res = max (res, yy + mid);
r = mid - 1;
}
}
cout << res << endl;
}
}