Even Digits
題目大意
給定一個數字n,找到相鄰最近的所有元素由偶數構成的數字,如22099,則為22088。
思路
題目比較簡單,找到一個比它大的數a及一個比它小的數b,比較差值即可。
其中,b可以通過找到最高元素為奇數的位,將其減1,并將之后所有低位全部賦值為8完成。
a則可以通過找到最高元素為奇數的位,將其加1,并將之后所有低位全部賦值為0完成,需要特別注意進位。
求解a的過程有一個優化點:如果需要進位(首個奇數位是9),(a-n) 一定大于 (n-b)。因為對于首字母為9的k位數,(a-n)一定大于(10k+1),而(n-b)則不會大于(10k+1)。
代碼
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#define ull unsigned long long
using namespace std;
const int MAXN = 20;
ull toNumber(char *str) {
ull res = 0;
for (int i = strlen(str)-1; i >= 0; --i) {
res = res * 10 + (str[i] - '0');
}
return res;
}
char * toStr(ull n) {
char *str = new char[MAXN];
for (int i = 0; i < MAXN; ++i) str[i] = '0';
int len = 0;
while (n != 0) {
str[len++] = (n % 10) + '0';
n /= 10;
}
str[len] = '\0';
return str;
}
char * getLower(char *num) {
int len = strlen(num) - 1;
for (; len >= 0; --len) {
if ((num[len] - '0') & 1) {
num[len--]--;
break;
}
}
while (len >= 0) num[len--] = '8';
return num;
}
char *getUpper(char *num) {
// 有優化點,首位為9時可以直接返回INT_MAX
int p = strlen(num) - 1;
for (; p >= 0; --p) {
if ((num[p] - '0') & 1) {
for (int i = 0; i < p; ++i) num[i] = '0';
if (num[p] == '9') {
num[p]++;
num[p+1]++;
num[p+2] = '\0';
return getUpper(num);
}
num[p]++;
break;
}
}
return num;
}
ull solve(ull n) {
ull a = toNumber(getLower(toStr(n)));
ull b = toNumber(getUpper(toStr(n)));
return min(n-a, b-n);
}
int main(int argc, const char * argv[]) {
int T;
ull n;
scanf("%d", &T);
for (int kase = 1; kase <= T; ++kase) {
scanf("%ld", &n);
printf("Case #%d: %llu\n", kase, solve(n));
}
return 0;
}
Lucky Dip
題目大意
袋子中有N個物品,每個物品值Vi元,現在選手從中取一個物品,共有k次放回機會。盡可能取出商品價值最大,求該數學期望。
思路
考慮一種放回策略,使得物品價值的期望最大,則可以考慮如下方案:
當本次取出商品價值大于之前幾次期望時,保留,否則放回重新取。
即K次循環,每次將商品價格排序后,依據上次的數學期望劃分成兩部分,價格低于期望的使用期望價格(放回),高于的部分用于更新本次期望(保留)。
狀態轉移方程:exp[i] = (smaller_count * exp[i-1] + sum_of_larger) / N;
預先排序便于二分,并計算前綴和。
代碼
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int MAXN = 2e4+10;
int V[MAXN];
double solve(int N, int K) {
sort(V, V+N);
ll *sum = new ll[N+1];
sum[N] = 0;
for (int i = N-1; i >= 0; --i)
sum[i] = sum[i+1] + V[i];
double *exp = new double[K+1];
exp[0] = (double) sum[0] / N;
for (int i = 1; i <= K; ++i) {
int larger = upper_bound(V, V+N, exp[i-1]) - V;
exp[i] = (larger * exp[i-1] + sum[larger]) / N;
}
return exp[K];
}
int main(int argc, const char * argv[]) {
int T, N, K;
scanf("%d", &T);
for (int kase = 1; kase <= T; ++kase) {
scanf("%d %d", &N, &K);
for (int i = 0; i < N; ++i) {
scanf("%d", &V[i]);
}
printf("Case #%d: %.6lf\n", kase, solve(N, K));
}
return 0;
}
Scrambled Words
題目大意
研表究明,漢字序順并不定一影閱響讀。事證實明了當你看這完句話之后才發字現都亂是的。
根據這一現象,Scrmable博士將一個單詞的首尾字母保持不變,中間字母任意亂序,生成的單詞成為Scrmable Word,現在我們認為原單詞和Scrmable單詞一樣可以閱讀。
現在給定一個字符串和一個單詞數組,求單詞數組中有多少個單詞出現在字符串中(原單詞和Scrmable單詞均可)。
思路
首尾不變,中間字母順序任意,則可以通過將單詞哈希來匹配,哈希依據字母表中每個字母出現的次數。
統計每個單詞的長度,遍歷所有存在的單詞長度,從頭到尾依次枚舉區間,當區間單詞哈希存在匹配時則存在Scrmabled或Original Word,匹配后移除匹配的單詞。
代碼
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
using namespace std;
const int HASH_SEED = 123;
const int MAXN = 1e5+10;
const int MAXM = 1e6+10;
int T, L, N, A, B, C, D;
char S1, S2;
char S[MAXM];
int numSeed[MAXM];
int alpha[26];
ull getHash(char start, char end, int alpha[]) {
ull res = start * HASH_SEED + end;
for (int i = 0; i < 26; ++i) {
res = res * HASH_SEED + alpha[i];
}
return res;
}
int solve() {
unordered_map<ull, int> hash;
int lenArr[MAXN];
char tmpStr[MAXN];
int totLen = 0;
scanf("%d", &L);
for (int i = 0; i < L; ++i) {
scanf("%s", tmpStr);
int len = strlen(tmpStr);
memset(alpha, 0, sizeof(alpha));
for (int j = 1; j < len-1; ++j)
alpha[tmpStr[j] - 'a']++;
hash[getHash(tmpStr[0], tmpStr[len-1], alpha)]++;
lenArr[totLen++] = len;
}
sort(lenArr, lenArr+totLen);
totLen = unique(lenArr, lenArr+totLen) - lenArr;
getchar();
scanf("%c %c %d %d %d %d %d", &S1, &S2, &N, &A, &B, &C, &D);
numSeed[0] = S[0] = S1;
numSeed[1] = S[1] = S2;
for (int i = 2; i < N; ++i) {
numSeed[i] = ((ll) A * numSeed[i-1] + (ll) B * numSeed[i-2] + (ll) C) % (ll) D;
S[i] = (char) (numSeed[i] % 26 + 97);
}
S[N] = '\0';
int res = 0;
ull substrHash;
for (int i = 0; i < totLen; ++i) {
int len = lenArr[i];
if (len > N) continue;
memset(alpha, 0, sizeof(alpha));
for (int j = 1; j < len-1; ++j)
alpha[S[j] - 'a']++;
substrHash = getHash(S[0], S[len-1], alpha);
if (hash.count(substrHash)) {
res += hash[substrHash];
hash.erase(substrHash);
}
for (int j = len; j < N; ++j) {
++alpha[S[j - 1] -'a'];
--alpha[S[j - len + 1] - 'a'];
substrHash = getHash(S[j-len+1], S[j], alpha);
if (hash.count(substrHash)) {
res += hash[substrHash];
hash.erase(substrHash);
}
}
}
return res;
}
int main(int argc, const char * argv[]) {
scanf("%d", &T);
for (int kase = 1; kase <= T; ++kase) {
printf("Case #%d: %d\n", kase, solve());
}
return 0;
}