HDU 4923 room and moor
Question: Provide sequence a to satisfy the B sequence written in the question to minimize the variance.
Ideas: we can think that the value of sequence B must be a segment in the final result, so we can look at it like greedy. For a sequence, we can find out the value of B to minimize the variance, that is, the value of 1 in the sequence. the number is divided by the sequence length. Because B is monotonous, we can use a monotonous stack to simulate the complexity. The complexity is far smaller than N ^ 2. Don't be scared...
Code:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 100010int t, n, top;int a[N], f[2][N];struct st { int head; double val;} g[N];int main() { int i, idx, num; double tmp1, ans; scanf("%d", &t); while (t--) { scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); f[0][i] = f[0][i - 1]; f[1][i] = f[1][i - 1]; f[a[i]][i]++; } f[0][n + 1] = f[0][n]; f[1][n + 1] = f[1][n]; g[0].head = 1; g[0].val = -1; top = 0; for (i = 1; i <= n; i++) { idx = i; tmp1 = a[i]; while (top >= 0 && g[top].val > tmp1) { idx = g[top].head; tmp1 = 1.0 * (f[1][i] - f[1][idx - 1]) / (i - idx + 1); top--; } top++; g[top].head = idx; g[top].val = tmp1; } ans = 0; g[top + 1].head = n + 1; for (i = 1; i <= top; i++) { tmp1 = g[i].val; num = f[0][g[i + 1].head - 1] - f[0][g[i].head - 1]; ans += tmp1 * tmp1 * num; num = f[1][g[i + 1].head - 1] - f[1][g[i].head - 1]; ans += (1.0 - tmp1) * (1.0 - tmp1) * num; } printf("%.6f\n", ans); } return 0;}
HDU 4925 apple tree
Q: N * m grids, either Apple or adjacent grids after fertilizer application. If Apple is planted, the number of apples doubles. Q: How many apples can be generated at most?
Idea: Obviously, tree planting and fertilization are based on the rectangle black and white dyeing method. In this case, the enumeration is the case of black grids or white grids, and the final selection of Max
Code:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int t, n, m;int main() { int i, j, ans, res, k; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); res = ans = 0; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { k = 1; if (i + 1 <= n) k *= 2; if (i - 1 >= 1) k *= 2; if (j + 1 <= m) k *= 2; if (j - 1 >= 1) k *= 2; if ((i + j) & 1) ans += k; else res += k; } } printf("%d\n", max(ans, res)); } return 0;}
HDU 4927 Series1
Question: each time a uses this equation AI = ai + 1-ai to make the number of numbers in the entire sequence-1 ask which number is left after N-1 operations
Idea: you only need to simplify the style to pay attention to high accuracy.
Code:
import java.io.*;import java.util.*;import java.math.*;public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int t, n, T, i, j; BigInteger[] a = new BigInteger[3010]; BigInteger[] c = new BigInteger[3010]; T = cin.nextInt(); for (t = 1; t <= T; t++) { n = cin.nextInt(); c[0] = BigInteger.ONE; for (i = 1; i <= n; i++) c[i] = c[i - 1].multiply(BigInteger.valueOf(n - i)).divide( BigInteger.valueOf(i)); for (i = 1; i <= n; i++) a[i] = cin.nextBigInteger(); BigInteger ans; ans = BigInteger.ZERO; int sign = 1; for (j = n; j >= 1; j--) { if (sign == 1) { ans = ans.add(a[j].multiply(c[n - j])); sign = -1; } else { ans = ans.subtract(a[j].multiply(c[n - j])); sign = 1; } } System.out.println(ans); } cin.close(); }}
HDU 4930 fighting the landlords
Q: landlords--B. If you win the cards with just one click or if you don't have the chance to win, ask if you can win.
Idea: Simulation questions... The logic is as follows:
First check whether the game can be completed once. If you cannot judge whether the King is in the hands of yourself, you must win in the hands of others.
If there is no final decision bomb, if there is no final result, it will be able to score all possible predictions.
Code:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int kind[10][5];int cas, flag;char str[20];int p1[20], p2[20], vis[20], only[10][5];int change(char x) {if (x == 'Y')return 17;else if (x == 'X')return 16;else if (x == '2')return 15;else if (x == 'A')return 14;else if (x == 'K')return 13;else if (x == 'Q')return 12;else if (x == 'J')return 11;else if (x == 'T')return 10;elsereturn x - '0';}bool cmp(int a, int b) {return a > b;}int main() {int i, j, len1, len2;scanf("%d", &cas);getchar();while (cas--) {memset(kind, -1, sizeof(kind));memset(only, 0, sizeof(only));flag = 0;gets(str);len1 = strlen(str);for (i = 0; i < len1; i++) {p1[i] = change(str[i]);}gets(str);len2 = strlen(str);for (i = 0; i < len2; i++) {p2[i] = change(str[i]);}sort(p1, p1 + len1, cmp);sort(p2, p2 + len2, cmp);//7if (len1 >= 2 && p1[0] == 17 && p1[1] == 16) {kind[7][1] = 1;// printf("1\n");printf("Yes\n");continue;}if (len2 >= 2 && p2[0] == 17 && p2[1] == 16) {kind[7][2] = 1;if (len2 == 2)only[7][2] = 1;}//8for (i = 3; i < len1; i++) {if (p1[i - 3] == p1[i - 2] && p1[i - 2] == p1[i - 1]&& p1[i - 1] == p1[i]) {kind[8][1] = p1[i];if (len1 == 4) {// printf("2\n");printf("Yes\n");flag = 1;}break;}}if (flag)continue;for (i = 3; i < len2; i++) {if (p2[i - 3] == p2[i - 2] && p2[i - 2] == p2[i - 1]&& p2[i - 1] == p2[i]) {kind[8][2] = p2[i];if (len2 == 4)only[8][2] = 1;break;}}if ((kind[8][1] > kind[8][2]) && (kind[7][2] == -1)) {// printf("3\n");printf("Yes\n");continue;}//1kind[1][1] = p1[0];kind[1][2] = p2[0];if (len1 == 1) {// printf("4\n");printf("Yes\n");continue;}if (kind[1][1] != -1 && kind[1][1] >= kind[1][2]) {if ((kind[7][2] == -1) && (kind[8][2] == -1)) {// printf("5\n");printf("Yes\n");continue;}}//2for (i = 1; i < len1; i++) {if (p1[i - 1] == p1[i]) {kind[2][1] = p1[i];if (len1 == 2)only[2][1] = 1;break;}}if (only[2][1]) {//printf("6\n");printf("Yes\n");continue;}for (i = 1; i < len2; i++) {if (p2[i - 1] == p2[i]) {kind[2][2] = p2[i];break;}}//printf("2....%d %d\n",kind[2][1],kind[2][2]);if (kind[2][1] != -1 && kind[2][1] >= kind[2][2]) {if ((kind[7][2] == -1) && (kind[8][2] == -1)) {//printf("7\n");printf("Yes\n");continue;}}//3for (i = 2; i < len1; i++) {if (p1[i - 1] == p1[i] && p1[i - 2] == p1[i - 1]) {kind[3][1] = p1[i];if (len1 == 3)only[3][1] = 1;break;}}if (only[3][1]) {//printf("8\n");printf("Yes\n");continue;}for (i = 2; i < len2; i++) {if (p2[i - 1] == p2[i] && p2[i - 2] == p2[i - 1]) {kind[3][2] = p2[i - 1];break;}}if (kind[3][1] != -1 && kind[3][1] >= kind[3][2]) {if ((kind[7][2] == -1) && (kind[8][2] == -1)) {//printf("9\n");printf("Yes\n");continue;}}//4for (i = 2; i < len1; i++) {if (p1[i - 1] == p1[i] && p1[i - 2] == p1[i - 1]) {if ((i + 1 < len1 && p1[i + 1] != p1[i])|| (i + 2 < len1 && p1[i + 2] != p1[i])|| (i - 3 >= 0 && p1[i - 3] != p1[i])|| (i - 4 >= 0 && p1[i - 4] != p1[i])) {kind[4][1] = p1[i];if (len1 == 4)only[4][1] = 1;break;}}}if (only[4][1]) {printf("Yes\n");continue;}for (i = 2; i < len2; i++) {if (p2[i - 1] == p2[i] && p2[i - 2] == p2[i - 1]) {if ((i + 1 < len2 && p2[i + 1] != p2[i])|| (i + 2 < len2 && p2[i + 2] != p2[i])|| (i - 3 >= 0 && p2[i - 3] != p2[i])|| (i - 4 >= 0 && p2[i - 4] != p2[i])) {kind[4][2] = p2[i];break;}}}if (kind[4][1] != -1 && kind[4][1] >= kind[4][2]) {if ((kind[7][2] == -1) && (kind[8][2] == -1)) {printf("Yes\n");continue;}}//5memset(vis, 0, sizeof(vis));for (i = 2; i < len1; i++) {if (p1[i - 1] == p1[i] && p1[i - 2] == p1[i - 1]) {vis[i - 2] = 1;vis[i - 1] = 1;vis[i] = 1;for (j = 1; j < len1; j++) {if ((!vis[j - 1]) && (!vis[j]) && p1[j] == p1[j - 1]) {kind[5][1] = p1[i];if (len1 == 5)only[5][1] = 1;break;}}vis[i - 2] = 0;vis[i - 1] = 0;vis[i] = 0;if (kind[5][1] != -1)break;}}if (only[5][1]) {printf("Yes\n");continue;}memset(vis, 0, sizeof(vis));for (i = 2; i < len2; i++) {if (p2[i - 1] == p2[i] && p2[i - 2] == p2[i - 1]) {vis[i - 2] = 1;vis[i - 1] = 1;vis[i] = 1;for (j = 1; j < len2; j++) {if ((!vis[j - 1]) && (!vis[j]) && p2[j] == p2[j - 1]) {kind[5][2] = p2[i];break;}}vis[i - 2] = 0;vis[i - 1] = 0;vis[i] = 0;if (kind[5][2] != -1)break;}}if (kind[5][1] != -1 && kind[5][1] >= kind[5][2]) {if ((kind[7][2] == -1) && (kind[8][2] == -1)) {printf("Yes\n");continue;}}//6for (i = 3; i < len1; i++) {if (p1[i - 3] == p1[i - 2] && p1[i - 2] == p1[i - 1]&& p1[i - 1] == p1[i]) {if (len1 >= 6) {kind[6][1] = p1[i];if (len1 == 6)only[6][1] = 1;break;}}}if (only[6][1]) {printf("Yes\n");continue;}for (i = 3; i < len2; i++) {if (p2[i - 3] == p2[i - 2] && p2[i - 2] == p2[i - 1]&& p2[i - 1] == p2[i]) {if (len2 >= 6) {kind[6][2] = p2[i];break;}}}if (kind[6][1] != -1 && kind[6][1] >= kind[6][2]) {if ((kind[7][2] == -1) && (kind[8][2] == -1)) {printf("Yes\n");continue;}}printf("No\n");}return 0;}