1. Niu Niu learns to shuffle
Analysis
According to the title, each time the first Xi cards and the remaining cards are separated, and then one by one from the two stacks of cards can be put back in turn.
Time complexity
O(n)
Reference Code
作者:NotDeep
链接:https://www.nowcoder.com/discuss/76124?type=0&order=0&pos=18&page=1
来源:牛客网
#include <bits/stdc++.h>
using namespace std;
int a[15];
int temp[2][15];
int len[2];
int main() {
int n;
bool f;
for(int i = 1; i <= 13; i++) scanf("%d", &a[i]);
scanf("%d", &n);
while(n--) {
scanf("%d", &len[0]);
len[1] = 13 - len[0];
for(int i = 1; i <= len[0]; i++) temp[0][i] = a[i];
for(int i = 1; i <= len[1]; i++) temp[1][i] = a[i + len[0]];
f = 0;
for(int i = 13; i >= 1; i--) {
if(len[f] == 0) f=!f;
a[i] = temp[f][len[f]];
len[f]--;
f = !f;
}
}
for(int i = 1; i <= 13; i++) {
printf("%d", a[i]);
if(i == 13)
printf("\n");
else
printf(" ");
}
return 0;
}
2. The largest subsequence
in
greedy. Each time, the character with the largest remaining lexicographical order is taken.
Time complexity
O(n)
Reference Code
作者:NotDeep
链接:https://www.nowcoder.com/discuss/76124?type=0&order=0&pos=18&page=1
来源:牛客网
#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
ostringstream ss;
while(!s.empty()) {
string::iterator it = max_element(s.begin(), s.end());
ss << *it;
s.erase(s.begin(), it + 1);
}
cout << ss.str() << endl;
return 0;
}
3.
Analysis
Scan one by one and count the number of different characters between the current character and the previous character.
Time complexity
O(n)
Reference Code
作者:NotDeep
链接:https://www.nowcoder.com/discuss/76124?type=0&order=0&pos=18&page=1
来源:牛客网
#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
int cnt = 0;
for(int i = 1; i < s.size(); i++) {
if(s[i - 1] == s[i]) {
cnt++;
i++;
}
}
cout << cnt << endl;
return 0;
}
4. Niu Niu and Doll
Analysis
According to the meaning of the question, there are only two weight dolls. For each weight, maintain the total number of dolls of this weight and the serial number of the last doll of this weight, and finally find the doll whose number is 1. The serial number is enough.
Time complexity
O(n)
Reference Code
作者:NotDeep
链接:https://www.nowcoder.com/discuss/76124?type=0&order=0&pos=18&page=1
来源:牛客网
#include <bits/stdc++.h>
using namespace std;
int w[1000005];
int main() {
int n;
int w1, w2, num1 = 0, num2 = 0, ans1, ans2;
scanf("%d", &n);
scanf("%d", &w[1]);
w1 = w[1];
num1 = 1;
ans1 = 1;
for(int i = 2; i <= n; i++) {
scanf("%d", &w[i]);
if(w[i] == w1) {
num1++;
ans1 = i;
} else {
w2 = w[i];
num2++;
ans2 = i;
}
}
if(num1 == 1)
printf("%d\n", ans1);
else
printf("%d\n", ans2);
return 0;
}
5. Three integers
Author : NotDeep
Link: https://www.nowcoder.com/discuss/76124?type=0&order=0&pos=18&page=1
Source: Niuke.com
Analysis
Let X be the number to which the last three numbers all become.
So the total number of operations required is (3X - (A + B + C)) / 2. Note that X must be greater than or equal to the maximum of the three numbers A, B, and C, we now want to minimize X, and neither operation changes the parity of A + B + C.
Let the maximum value of A, B, C be Ma = max(A, max(B, C))
so if 3 Ma has the same parity as A + B + C, X = Ma, otherwise X = Ma + 1.
Then output (3Ma - (A + B + C)) / 2.
Time complexity
O(1)
Reference Code
作者:NotDeep
链接:https://www.nowcoder.com/discuss/76124?type=0&order=0&pos=18&page=1
来源:牛客网
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[3];
cin >> a[0] >> a[1] >> a[2];
sort(a, a + 3);
if ((a[2] - a[1] + a[2] - a[0]) % 2 == 0)
cout << (a[2] - a[1] + a[2] - a[0]) / 2;
else
cout << (a[2] - a[1] + a[2] - a[0] + 3) / 2;
return 0;
}
6. The
analysis
can be solved by the generating function or directly with the backpack dp.
Reference Code
作者:NotDeep
链接:https://www.nowcoder.com/discuss/76124?type=0&order=0&pos=18&page=1
来源:牛客网
#include <bits/stdc++.h>
using namespace std;
long long f[2][105];
int main() {
int n, m;
scanf("%d%d", &n, &m);
memset(f, 0, sizeof(f));
f[0][0] = 1;
for (int i = 1; i <= n; i++) {
memset(f[i & 1], 0, sizeof(f[i & 1]));
int l, r;
scanf("%d%d", &l, &r);
for (int k = l; k <= r; k++)
for (int j = m; j >= k; j--)
f[i & 1][j] += f[i + 1 & 1][j - k];
}
printf("%lld\n", f[n & 1][m]);
return 0;
}
7. Niu Niu distributes snacks
Author : NotDeep
Link: https://www.nowcoder.com/discuss/76124?type=0&order=0&pos=18&page=1
Source: Niuke.com
Analysis There are three types of
legal snack boxes: one sweet, one bitter and one sour/two sweets and one bitter (sour)/three sweets.
In order to maximize the number of dessert boxes, we must give priority to forming the first type of dessert box, then consider the second type of dessert box, and finally the remaining dessert boxes form a dessert box every three.
So the question becomes, how many pairs of bitter and sour desserts can be formed at the same time, and each pair of desserts does not conflict.
We can use the bipartite graph matching model to consider connecting an edge between any pair of non-conflicting desserts, one bitter and one sour, and then find the maximum matching.
Finally, the answer is calculated according to the maximum matching number, the number of extra bitter (sour) desserts, and the number of sweet desserts.
Time complexity
O(n^3)
Reference Code
作者:NotDeep
链接:https://www.nowcoder.com/discuss/76124?type=0&order=0&pos=18&page=1
来源:牛客网
#include <bits/stdc++.h>
using namespace std;
char s[505][5];
bool ok[505][505];
int root[505];
vector <int> vec[505];
bool life[505];
bool solve(int x) {
life[x] = 1;
for(int i = 0; i < vec[x].size(); i++) {
if(life[vec[x][i]])
continue;
else
life[vec[x][i]] = 1;
if(!root[vec[x][i]] || solve(root[vec[x][i]])) {
root[vec[x][i]] = x;
return 1;
}
}
return 0;
}
int main() {
int n, m, u, v;
int numdl = 0, numdc = 0, numds = 0, ans = 0;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%s", s[i]);
for(int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
ok[u][v] = 1;
ok[v][u] = 1;
}
for(int i = 1; i <= n; i++) {
if(s[i][0] == 'K') {
numdc++;
for(int j = 1; j <= n; j++) {
if(s[j][0] == 'S' && !ok[i][j])
vec[i].push_back(j);
}
} else {
if(s[i][0] == 'T') numdl++;
else numds++;
}
}
for(int i = 1; i <= n; i++) {
memset(life, 0, sizeof(life));
if(s[i][1] == 'C' && solve(i)) ans++;
}
if(ans >= numdl)
printf("%d\n", numdl);
else
{
if((numdl - ans) / 2 >= (numdc + numds - ans * 2))
printf("%d\n", numdc + numds - ans + (numdl - ans - (numdc + numds - ans * 2) * 2) / 3);
else
printf("%d\n", ans + (numdl - ans) / 2);
}
return 0;
}