Codeforces Round #197 (Div. 2)
分析:将读入的字符转化为数字,直接排个序就可以了。
#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 ;
}
分析:模拟即可,大于当前位置直接走,否则绕一圈后走到目标位置。
#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 ;
}
分析:直接搜索即可,无法证明为何会如此快的找到答案或者退出。
#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 ;
}
分析:由于每次改变的位置只会改变与其相关位置的变化,对于其余部分的结果可以使用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 ;
}
Codeforces Round #197 (Div. 2)的更多相关文章
- 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...
- Codeforces Round #197 (Div. 2) : E
看了codeforces上的大神写的题解之后,才知道这道题水的根本! 不过相对前面两题来说,这道题的思维要难一点: 不过想到了水的根本,这题也真心不难: 方法嘛,就像剥洋葱一样,从外面往里面剥: 所以 ...
- [置顶] Codeforces Round #197 (Div. 2)(完全)
http://codeforces.com/contest/339/ 这场正是水题大放送,在家晚上限制,赛后做了虚拟比赛 A,B 乱搞水题 C 我是贪心过的,枚举一下第一个拿的,然后选使差值最小的那个 ...
- Codeforces Round #197 (Div. 2) (A、B、C、D、E五题合集)
A. Helpful Maths 题目大意 给一个连加计算式,只包含数字 1.2.3,要求重新排序,使得连加的数字从小到大 做法分析 把所有的数字记录下来,从小到大排序输出即可 参考代码 #inclu ...
- Codeforces Round #197 (Div. 2) C,D两题
开了个小号做,C题一开始看错范围,D题看了半小时才看懂,居然也升到了div1,囧. C - Xenia and Weights 给出一串字符串,第i位如果是1的话,表示有重量为i的砝码,如果有该种砝码 ...
- Codeforces Round #197 (Div. 2) : C
哎....这次的比赛被安叔骂的好惨! 不行呢,要虐回来: 这道搜索,老是写错,蛋疼啊! 果然是基础没打好! #include<cstdio> using namespace std; ], ...
- Codeforces Round #197 (Div. 2) : D
这题也是一个线段树的水题: 不过开始题目没看明白,害得我敲了一个好复杂的程序.蛋疼啊.... 最后十几分钟的时候突然领悟到了题意,但是还是漏掉一个细节,老是过不去... 以后比赛的时候不喝啤酒了,再也 ...
- Codeforces Round #197 (Div. 2) : B
也是水题一个,不过稍微要细心点.... 贴代码: #include<iostream> using namespace std; long long n,m; ; int main() { ...
- Codeforces Round #197 (Div. 2) : A
水题一个: 直接贴代码: #include<cstdio> #include<algorithm> #include<cstring> using namespac ...
随机推荐
- 用Jquery控制文本框只能输入数字和字母
用Jquery控制文本框只能输入数字和字母 $.fn.onlyNum = function () { $(this).keypress(function (event) { var eventObj ...
- tcpproxy:基于 Swoole 实现的 TCP 数据包转发工具的方法
假设我们希望有一台机器A(ip 192.168.1.101)要开放端口6379给用户访问,但可能实际情况是用户无法直接访问到A(ip 192.168.1.101), 但却有一台机器B(ip 192.1 ...
- The C++ Standard Library --- A Tutorial Reference 读书笔记
5.2 Smart Pointer(智能指针) shared_ptr的aliasing构造函数,接受一个shared pointer和一个raw pointer.它允许你掌握一个事实:某对象拥有另一个 ...
- java中方法的参数传递机制(值传递还是引用传递)
看到一个java面试题: 问:当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递? 答:是值传递.Java 编程语言只有值传递参 ...
- JavaScript脚本语言基础(二)
导读: JavaScript条件语句 JavaScript循环语句 JavaScript网页中错误捕获 JavaScript的Break和Continue命令 JavaScript的转义字符 1.Ja ...
- cocos2dx资源和脚本加密quick-lua3.3final
一.资源加密 版本号:Quick-Cocos2d-x 3.3 Final 调试工具:xCode 工程创建的时候选择的拷贝源码. 项目结构如图: 这个功能七月大神在很早之前就已经实现了,但是在3.3版本 ...
- android 代码整体回退
repo forall -c 'HAHA=`git log --before="3 days" -1 --pretty=format:"%H"`;git res ...
- SDUT 2413:n a^o7 !
n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you w ...
- POJ 2001:Shortest Prefixes
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16782 Accepted: 728 ...
- java实现贪吃蛇游戏
最简单的4个java类就可以实现贪吃蛇: main函数: package tcs; public class GreedSnake { public static void main(String[] ...