A. Even odds
Here we will give you N and K to arrange an even number in the odd row from 1 to n in a new sequence and output the number at the K position.
For example, after 10 3 beats, the number of 1 3 5 7 9 2 4 6 8 10 3rd is 5.
//cf 318 A//2013-06-18-20.30#include <iostream>using namespace std;int main(){ __int64 n, k; while (cin >> n >> k) { if (k <= (n+1)/2) cout << (k-1)*2 + 1 << endl; else { k -= (n+1)/2; cout << k*2 << endl; } } return 0;}
B. sereja and array
Find the number of strings starting with "heavy" and ending with "Metal.
My idea is to mark the locations of "heavy" and "Metal" and then calculate the number of parts ending with each "Metal" and then add them together.
//cf 318 B//2013-06-18-21.01#include <stdio.h>#include <string.h>#include <iostream>using namespace std;const int maxn = 1000006;char str[maxn];int s[maxn];int e[maxn];int main(){ while (scanf("%s", str) != EOF) { memset(s, 0, sizeof(s)); memset(e, 0, sizeof(e)); int l = strlen(str); for (int i = 0; i < l-4; i++) { if (str[i] == 'h' && str[i+1] == 'e' && str[i+2] == 'a' && str[i+3] == 'v' && str[i+4] == 'y') { s[i] = 1; i += 4; continue; } if (str[i] == 'm' && str[i+1] == 'e' && str[i+2] == 't' && str[i+3] == 'a' && str[i+4] == 'l') { e[i] = 1; i += 4; continue; } } __int64 ans = 0; for (int i = 1; i < l; i++) { if (e[i]) ans += s[i-1]; s[i] += s[i-1]; } cout << ans << endl; } return 0;}