A. Square
思路:按横坐标进行排序,边长一定是前两个元素的纵坐标之差
#include <bits/stdc++.h>
using namespace std;
#pragma G++ optimize(2)
#define double long double
#define int long long
#define xiaowen ac
typedef unsigned long long ull;
typedef long long ll;
const double eps = 1e-8;
const int N = 3e6 + 7;
const int P = 131;
struct node
{
int x, y;
} pl[4];
bool cmp(node k1, node k2)
{
return k1.x < k2.x;
}
void solve()
{
for (int i = 0; i < 4; i++)
{
cin >> pl[i].x >> pl[i].y;
}
sort(pl, pl + 4, cmp);
int t = abs(pl[1].y - pl[0].y);
int res = t * t;
cout << res << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
// T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
B. Arranging Cats
思路:贪心,能用操作3就用
#include <bits/stdc++.h>
using namespace std;
#pragma G++ optimize(2)
#define double long double
#define int long long
#define xiaowen ac
typedef unsigned long long ull;
typedef long long ll;
const double eps = 1e-8;
const int N = 3e6 + 7;
const int P = 131;
string s, t;
int n;
void solve()
{
cin >> n;
cin >> s >> t;
int c1 = 0, c2 = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] != t[i])
{
if (s[i] == '1' && t[i] == '0')
{
c1++;
}
else
{
c2++;
}
}
}
int res = min(c1, c2) + max(c1, c2) - min(c1, c2);
cout << res << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
// T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
C. Sending Messages
思路:根据题意模拟即可 如果两个消息之间的时间间隔t*a<=b就开机,否则就关机
#include <bits/stdc++.h>
using namespace std;
#pragma G++ optimize(2)
#define double long double
#define int long long
#define xiaowen ac
typedef unsigned long long ull;
typedef long long ll;
const double eps = 1e-8;
const int N = 3e6 + 7;
const int P = 131;
int n, f, a, b;
int c[N];
void solve()
{
cin >> n >> f >> a >> b;
for (int i = 1; i <= n; i++)
{
cin >> c[i];
}
int res = 0;
c[0] = 0;
for (int i = 1; i <= n; i++)
{
int t = c[i] - c[i - 1];
int k1 = t * a;
res += min(k1, b);
}
// cout << "KK " << res << '\n';
if (res >= f)
{
cout << "NO" << '\n';
}
else
{
cout << "YES" << '\n';
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
// T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
D. Very Different Array
思路:双指针加贪心
#include <bits/stdc++.h>
using namespace std;
#pragma G++ optimize(2)
#define double long double
#define int long long
#define xiaowen ac
typedef unsigned long long ull;
typedef long long ll;
const double eps = 1e-8;
const int N = 3e6 + 7;
const int P = 131;
int n, m;
int a[N];
int b[N];
void solve()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= m; i++)
{
cin >> b[i];
}
sort(a + 1, a + 1 + n);
sort(b + 1, b + 1 + m);
int z1 = 1, z2 = n;
int z3 = 1, z4 = m;
int res = 0;
while (z1 <= z2)
{
if (z1 == z2)
{
int k1 = abs(b[z3] - a[z1]);
int k2 = abs(b[z4] - a[z1]);
res += max(k1, k2);
break;
}
else
{
int k1 = abs(b[z3] - a[z1]);
int k2 = abs(b[z4] - a[z1]);
int k3 = abs(b[z3] - a[z2]);
int k4 = abs(b[z4] - a[z2]);
int k6 = max(k1, k2);
int k7 = max(k3, k4);
if (k6 >= k7)
{
res += k6;
if (k1 >= k2)
{
z3++;
}
else
{
z4--;
}
z1++;
}
else
{
res += k7;
if (k3 >= k4)
{
z3++;
}
else
{
z4--;
}
z2--;
}
}
}
cout << res << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
// T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}