A.Helpful Maths

分析:将读入的字符转化为数字,直接排个序就可以了。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = ;
int seq[N]; int main() {
char c;
int t = , idx = ;
while ((c = getchar()) != EOF) {
if (c == '+' || c == '\n') {
seq[idx++] = t;
t = ;
}
else t = t * + c - '';
}
sort(seq, seq+idx);
printf("%d", seq[]);
for (int i = ; i < idx; ++i) printf("+%d", seq[i]);
puts("");
return ;
}

B.Xenia and Ringroad

分析:模拟即可,大于当前位置直接走,否则绕一圈后走到目标位置。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; typedef long long LL;
int n, m; int main() {
while (scanf("%d %d", &n, &m) != EOF) {
int last = , x;
LL ret = ;
while (m--) {
scanf("%d", &x);
if (x >= last) {
ret += x - last;
last = x;
} else {
ret += n-last+x;
last = x;
}
}
printf("%I64d\n", ret);
}
return ;
}

C.Xenia and Weights

分析:直接搜索即可,无法证明为何会如此快的找到答案或者退出。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = ;
char str[];
int m;
int path[N]; bool dfs(int p, int left, int right, int last) {
if (p == ) return true;
int delta = abs(left-right);
for (int i = ; i <= ; ++i) {
if (last == i) continue;
if (str[i] && delta >= && delta < i) {
path[p] = i;
if (left < right) {
if (dfs(p-, left+i, right, i)) return true;
}
else if (dfs(p-, left, right+i, i)) return true;
}
}
return false;
} int main() {
scanf("%s", str+);
scanf("%d", &m);
for (int i = ; i <= ; ++i) str[i] -= '';
if (!dfs(m, , , )) {
puts("NO");
} else {
puts("YES");
for (int i = m; i > ; --i) {
printf(i == m ? "%d" : " %d", path[i]);
}
puts("");
}
return ;
}

D.Xenia and Bit Operations

分析:由于每次改变的位置只会改变与其相关位置的变化,对于其余部分的结果可以使用map存储起来,写的时候对log2(x)没有处理好,导致精度误差,教训啊。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std; const int N = (<<)+;
int n, m;
int seq[N];
int cpy[N];
map<pair<int,int>, int>mp;
map<pair<int,int>, int>::iterator it; int cal(int p, int l, int r) {
if (r - l == ) return mp[make_pair(l, r)] = (seq[l] | seq[r]);
int mid = (r+l) >> ;
int f = (int)floor(log((r-l+)*1.0)/log(2.0)+0.5); // 直接除取整会带来精度丢失
if (p <= mid) {
if (f & ) {// or
return (mp[make_pair(l, mid)] = cal(p, l, mid)) | mp[make_pair(mid+, r)];
} else {
return (mp[make_pair(l, mid)] = cal(p, l, mid)) ^ mp[make_pair(mid+, r)];
}
} else {
if (f & ) {
return mp[make_pair(l, mid)] | (mp[make_pair(mid+, r)] = cal(p, mid+, r));
} else {
return mp[make_pair(l, mid)] ^ (mp[make_pair(mid+, r)] = cal(p, mid+, r));
}
}
} int main() {
scanf("%d %d", &n, &m);
int LIM = << n;
for (int i = ; i <= LIM; ++i) {
scanf("%d", &seq[i]);
cpy[i] = seq[i];
}
for (int i = , k = ; i < LIM; i <<= , ++k) {
for (int j = ; j+i <= LIM; j += (i<<)) {
if (k & ) { // xor
cpy[j] ^= cpy[j+i];
mp[make_pair(j, j+(i<<)-)] = cpy[j];
} else { // or
cpy[j] |= cpy[j+i];
mp[make_pair(j, j+(i<<)-)] = cpy[j];
}
}
}
int p, b;
while (m--) {
scanf("%d %d", &p, &b);
seq[p] = b;
printf("%d\n", cal(p, , LIM));
}
return ;
}
04-17 01:12