2014多校聯合五(HDU 4911 HDU 4915 HDU 4920),hdu4911
HDU 4911 Inversion
題意:n個數字 通過k次相鄰交換 使得逆序對數最少
思路:如果序列為 XXXABYYY 假設A和B位置互換 易知X和AB、Y和AB的逆序對數不變 換句話說一次交換最多使逆序對減少1 那麼只需要求原逆序對數和k進行比較即可
代碼:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 100100typedef __int64 ll;int n;ll k, ans;int a[N], b[N], c[N];int lowbit(int x) { return x & (-x);}void add(int x) { for (; x <= n; x += lowbit(x)) { c[x]++; }}int sum(int x) { int res = 0; for (; x > 0; x -= lowbit(x)) { res += c[x]; } return res;}int main() { int i, j; while (~scanf("%d%I64d", &n, &k)) { for (i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } memset(c, 0, sizeof(c)); sort(b + 1, b + n + 1); ans = 0; for (i = 1; i <= n; i++) { j = lower_bound(b + 1, b + n + 1, a[i]) - b; ans += sum(n) - sum(j); add(j); } if (ans > k) printf("%I64d\n", ans - k); else printf("0\n"); } return 0;}
HDU 4915 Parenthese sequence
題意:?可以代表(或) 那麼輸入的字串能構造出幾種合法的括弧序列呢 輸出無解、唯一解、多解
思路:這題是我YY的… 首先我們可以計算出(和)應該填幾個 如果計算出?不滿足我們要填的東西 那麼必然無解 接著有一種最簡單的解 那就是把靠左邊的問號全變成‘(’(注意個數)剩下的變成‘)’ 如果這樣構造完的字串都不是合法的 那麼一定沒有合法的解 然後開始YY… 發現我們填上的最中間的(和)通過交換可以影響最小 而且兩邊的括弧很可能可以滿足它們 所以我們將構造出的序列中我們填上的最中間的兩個括弧交換 最後驗證一下新串是否合法 合法就是多解 否則唯一解 注意!! 有可能我們根本不能交換!! 因為我們全填的是(或)
代碼:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 1000010char str[N], f[N];int n, num, g1, g2;int ff(int x) { if (x < 0) return -x; return x;}int main() { int i, j, tmp; while (~scanf("%s", str)) { n = strlen(str); for (i = num = g1 = g2 = 0; i < n; i++) { if (str[i] == '?') num++; else if (str[i] == '(') g1++; else g2++; } tmp = ff(g1 - g2); if (num < tmp || (num - tmp) % 2 != 0) { puts("None"); continue; } if (g1 < g2) tmp = tmp + (num - tmp) / 2; else tmp = (num - tmp) / 2; for (i = j = 0; i < n; i++) { if (str[i] == '?') { j++; if (j <= tmp) str[i] = '('; else str[i] = ')'; if (j < tmp || j == tmp + 1) f[i] = '('; else f[i] = ')'; } else f[i] = str[i]; } for (i = j = 0; i < n; i++) { if (str[i] == '(') j++; else j--; if (j < 0) break; } if (i < n) { puts("None"); continue; } if (tmp == 0 || tmp == num) { puts("Unique"); continue; } for (i = j = 0; i < n; i++) { if (f[i] == '(') j++; else j--; if (j < 0) break; } if (i < n) puts("Unique"); else puts("Many"); } return 0;}
HDU 4920 Matrix multiplication
題意:求矩陣乘法 最後所有數字%3
思路:分塊! 如果n3我們會T的話 那麼我們可以把任務分成兩次去做 類似於首碼和或分治的最佳化思想!
首先分塊7個數字一組 然後矩陣相乘就變成了塊相乘 既然塊相乘的複雜度我們可以接受 那麼剩下的就是——如果給你兩個塊 如何快速計算他們的積 打表! 我們利用3進位數表示狀態dp打出任何兩個塊相乘的積 最後利用dp的結果計算即可
代碼:
#include<cstring>#include<cstdio>#include<algorithm>using namespace std;#define M 810int n, N;int f[2200][2200];int a[M][M], b[M][M], c[M][M];int A[M][M], B[M][M];int read() { char ch; bool ok = 0; int res = 0; for (;;) { ch = getchar(); if (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ok = 1; else if (ok) return res % 3; }}int solve(int a, int b) { int i = 7, res = 0; while (i--) { res += (a % 3) * (b % 3); a /= 3; b /= 3; } return res % 3;}int main() { int i, j, k; for (i = 0; i < 2187; i++) for (j = 0; j < 2187; j++) f[i][j] = solve(i, j); while (~scanf("%d", &n)) { N = (n - 1) / 7 + 1; for (i = 0; i < n; i++) { int cnt = 0; int tmp = 0; for (j = 0; j < n; j++) { cnt++; a[i][j] = read(); tmp = tmp * 3 + a[i][j]; if (cnt == 7) { A[i][j / 7] = tmp; tmp = 0; cnt = 0; } } if (cnt > 0) A[i][N - 1] = tmp; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) { b[i][j] = read(); c[i][j] = 0; } for (j = 0; j < n; j++) { int cnt = 0; int tmp = 0; for (i = 0; i < n; i++) { cnt++; tmp = tmp * 3 + b[i][j]; if (cnt == 7) { B[i / 7][j] = tmp; tmp = 0; cnt = 0; } } if (cnt > 0) B[N - 1][j] = tmp; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < N; k++) c[i][j] += f[A[i][k]][B[k][j]]; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { printf("%d", c[i][j] % 3); if (j == n - 1) { putchar('\n'); } else putchar(' '); } } return 0;}
PS:GXX姐姐出的題還是很良心的 感覺這次比賽值得進一步去做! 雖然弱弱的自己比賽只出了3題…TAT
hdu2014 青年歌手大獎賽_評委會打分,為何我的程式是 WrongAnswer?
這樣就可以 AC了
#include <stdio.h>
void main()
{
int n,i;
double score;//定義一個變數就可以了,不用數組
double average,min,max,sum;
while(scanf("%d",&n)!=EOF)
{
sum=0;min=101;max=0;//min要初始化大於100
for(i=1;i<=n;i++)
{
scanf("%lf",&score);
if(min>score) min=score;
if(max<score) max=score;
sum+=score;
}
average=(sum-min-max)/(n-2);
printf("%.2lf\n",average);
}
}
acm hdu 2014
題目有問題!
題目有一個範例的n是100
你把while(cin>>n,n>2&&n<100)中的,n>2&&n<100就能AC