Article directory
A – A Xor B Problem
topic analysis
Only the XOR result of the same number is zero, count the number of occurrences of the same number, and arrange and combine.
According to the example, self and self can become a pair of numbers.
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1010;
int n, m, k, t;
int a[N];
map<int, int>q;
signed main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
q[a[i]] ++;
}
int ans = 0;
for(auto &[k, v] : q)
{
if(v >= 2) ans += v * v;
else ans ++;
}
cout << ans << "\n";;
return 0;
}
B - eat an apple
topic analysis
It can be sorted by the difference between the pleasure value of eating apples in the morning and at night. The larger the difference, the higher the priority and the greater the contribution value.
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, m, k, t;
bool st[N];
struct node
{
int l, r;
}q[N];
bool cmp(node a, node b)
{
return abs(a.l - a.r) > abs(b.l - b.r);
}
signed main()
{
cin >> n >> k;
for(int i = 1; i <= n; i ++)
{
int u, v;
cin >> u >> v;
q[i] = {
u, v};
}
sort(q + 1, q + n + 1, cmp);
int ans = 0;
int r1 = n - k, r2 = k;
for(int i = 1; i <= n; i ++)
{
if(q[i].l > q[i].r)
{
if(r1)
{
ans += q[i].l;
r1 --;
}
else ans += q[i].r, r2 --;
}
else
{
if(r2)
{
ans += q[i].r;
r2 --;
}
else ans += q[i].l, r1 --;
}
}
cout << ans << "\n";
return 0;
}
C – n queens problem
topic analysis
Every time a point is input, it is enough to judge whether it has been let go in the eight directions. However, when judging, the violent method will time out. We can judge whether there will be a conflict by judging whether it is on a straight line.
The horizontal and vertical ones are relatively simple, the problem is to deal with the two diagonals. The two diagonals are y=x+a
the sum y=-x+b
, x
and you can y
judge whether they are on a straight line by checking whether the constants are the same.
code
#include<bits/stdc++.h>
using namespace std;
const int N = 1e7 + 10;
int n, m, k, t;
bool row[N], col[N], dg[N], udg[N];
bool get(int x, int y)
{
if(!row[x] && !col[y] && !dg[x + y] && !udg[n - x + y])
{
row[x] = col[y] = dg[x + y] = udg[n - x + y] = true;
return true;
}
return false;
}
signed main()
{
scanf("%d%d", &n, &t);
while(t --)
{
int x, y;
scanf("%d%d", &x, &y);
if (get(x, y)) puts("Yes");
else puts("No");
}
return 0;
}
D – Divide apples
topic analysis
It can be seen as two wooden sticks dividing a desktop into four parts, and the point coordinates can be used to determine which part it is based on the numerical value.
code
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[5];
int n, m, k, t;
int Ae, Be, Ce;
int Ar, Br, Cr;
signed main()
{
cin >> n;
cin >> Ae >> Be >> Ce;
cin >> Ar >> Br >> Cr;
for(int i = 1; i <= n; i ++)
{
int x, y;
cin >> x >> y;
int ans1 = Ae * x + Be * y + Ce;
int ans2 = Ar * x + Br * y + Cr;
if(ans1 > 0 && ans2 > 0) a[1]++;
else if(ans1 > 0 && ans2 < 0) a[2]++;
else if(ans1 < 0 && ans2 > 0) a[3] ++;
else if(ans1 < 0 && ans2 < 0) a[4] ++;
}
sort(a + 1, a + 5);
for(int i = 1; i <= 4; i ++) cout << a[i] << " " ;
}
E – Cloze
topic analysis
Ontology data range is small and divided into many situations, dynamic programming method can be used.
Set f[i][j][k][r]
means: choose i for option A, choose j for option B, choose k for option C, and choose f for option D, and get the set of expected values
According to the method of thinking the previous one, the state transition equation can be easily obtained.
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 110;
int n, m, k, t;
int a[N], w[N][5];
int f[N][N][N][N];
signed main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= 4; j ++)
cin >> w[i][j];
n /= 4;
for(int i = 0; i <= n; i ++)
for(int j = 0; j <= n; j ++)
for(int k = 0; k <= n; k ++)
for(int r = 0; r <= n; r ++)
{
if(i > 0)
f[i][j][k][r] = max(f[i][j][k][r], f[i - 1][j][k][r] + w[i + j + k + r][1]);
if(j > 0)
f[i][j][k][r] = max(f[i][j][k][r], f[i][j - 1][k][r] + w[i + j + k + r][2]);
if(k > 0)
f[i][j][k][r] = max(f[i][j][k][r], f[i][j][k - 1][r] + w[i + j + k + r][3]);
if(r > 0)
f[i][j][k][r] = max(f[i][j][k][r], f[i][j][k][r - 1] + w[i + j + k + r][4]);
}
cout << f[n][n][n][n] << "\n";
return 0;
}