aoapc-book / aoapc-bac2nd Goto Github PK
View Code? Open in Web Editor NEWSource codes for book <<<BeginningAlgorithmContests>> Second edition
Source codes for book <<<BeginningAlgorithmContests>> Second edition
表10-5第四列的gcd值应为8,i的值应为6,书中应该上下颠倒了
In line 17
if(x[i+1] != (a*x[i] + b) % M) { ok = false; break; }
should be
if(i+1 <= T*2 && x[i+1] != (a*x[i] + b) % M) { ok = false; break; }
And in line 27
for(int i = 2; i <= T*2; i++) cout << x[i] << "\n";
should be
for(int i = 2; i <= T*2; i+=2) cout << x[i] << "\n";
例题6-11 UVa297
maxn必须开大一点,如果是1024+10会WA,因为输入数据有比这个长的
page264页的dp函数,没有s=0时的出口函数,所以把ans设为0和(1<<30)都应该是一样的,都无法区分出出无法到达和已到达重点
即使科学上网,网速也非常慢,大家都是在哪里提交代码的?
Although the code can get AC, I think the >=
should be >
in line 48. >
means starting from a circle that intersects the upper border, while >=
allows to start from a tangent circle.
if(y[i] + r[i] >= W && dfs(i)) { ok = false; break; } // 从上往下dfs
A simple test case below will make the code get WA. That is, you can go along the upper border, but the code will give "IMPOSSIBLE".
2
500 500 500
500 0 100
在提示2-3下面的那段正文中,原文表述为“变量i定义在循环语句中,因此i在循环体内不可见
这段正文指的代码是
for(int i=1;i<=n;i++) printf("%d\n",i;
循环体内i应当是可见的
入门经典授课教案第二章习题2-9 分数化小数 (P/35 习题2-5)程序有bug,对四舍五入的进位考虑不完善,例如:1.9999 保留三位有效数字答案就是错的
我已經用手寫寫出答案,書本:打下好基礎程式設計與演算法競賽--入門經典(繁體書,)但因沒時間上機慢慢測驗是否正確,其他章節是否也請作者給完整的解答,以方便驗證是否正確,還有官方放的友情連結網友給的習題答案已經失效,可否也請修復一下呢? 目前請作者請先給第一章解答,謝謝!
按照题目给的算法,当答案序列为(1,3,5,5),测试序列为(5,5,1,1) 时,
程序得出的答案是(0,3)
但是实际上答案是不是应该为(0,2)?
所以算法是不是结构有问题?
Line98: edges.size();
should it be edges.clear(); ?
In line 18
if(a[p] == x)
should be
if(p < n && a[p] == x)
Although the code is Accepted, I think it will meet problem in some cases.
虽然代码中没有用到g(k, i)这个函数,但其计算方式出现错误
if (i >= k2) return g(k - 1, i - k2) + count(k - 1);这是源代码中的那个式子
但实际应为if (i >= k2) return g(k - 1, i - k2) * 2 + count(k - 1);
仔细想象一下并不困难,左下角一个整体的方块,加上上面两块残缺的区域
这道题用的f(k, b) - f(k, a - 1)来表示的答案
同理答案也等于g(k, l - a + 1) - g(k, l - b), l is 1 << k
由于Google Code挂了现在不知道在哪里开issues... Po在这里试试作者能不能看到
Correct me if I were wrong/if我买到了盗版= =
【分析】第一句话应该是“后序遍历的最后一个字符就是根”,而不是第一个字符。
主函数里面有一行edges.size();这行没任何作用啊
书中说其他的运算会放到代码仓库……于是烂尾了?
于是烂尾了??
How Many Pieces of Land? 这题
书中有一个猜想,每次总是使用”刚刚得到“的那个数。
这个猜想可以尝试用反证法证明,不知道是否正确。
假设目标数X最少需要d次乘除法求出,那么在解答树上,X位于深度d,则根据猜想假设,X一定由X(d-1)求出: X = X(d-1) +/- Xk,0 <= k <= d-1。
现在给出反例,X无法由Xd-1得到,但可以由另外路径中两个结点Xi, Xj得到,即X = Xi +/- Xj,0 <= i <= j < d-1,那么在另一条路径,0--i--j,X = X = Xi +/- Xj,那X可以在第j+1层就被求出,即只需要j+1 次乘除法就可以求出,与假设最少需要d(>j+1)次乘除法相悖,因此反例不存在。
复制题解代码到uva上提交常会出错,通过不了;
有时把代码放到自己的编译器里编译后输出的结果和uva上给出的标准结果不相同。
P124代码下面那行有误,这里是以1e8为一“位”,而不是10。举例:若要表示90901234432156788765,则s={9090,12344321,56788765}
只找到6道例题答案,那12道习题答案在哪?
if (!vis[0][i] && !vis[1][cur+i] && !vis[2][cur-i+n]
can be optimized to
if (!vis[0][i] && !vis[1][cur+i-n] && !vis[2][cur-i+n]
in prevention in memory abuse.
e.g. cur+i maybe is bigger than the limit for N Queen Problem. Maybe subtract an n on that expression.
书中提及”上述算法非常直观,正确性却不是显然的“,请问为什么无法严格证明它是正确的?
算法使用priority queue,可以保证每次都pop出dist
最小的node u
,而每次把u
的子节点u2
push进priority queue的时候,u2.dist = u.dist + amount
,所以可以严格保证u2.dist > u.dist
。
所以当u
被pop出,u.v
中三个杯子水量的dist (ans[u.v[i]])
,如果不是-1
(初始值,没被填充过),则一定是可达到的最小值,因为u.dist
是当前所有node中dist
的最小值,而在尚未push进priority queue的state node u2
,所有的u2.dist
都严格大于u.dist
。
此外在update_ans()
函数中,
if (ans[d] < 0 || u.dist < ans[d]) ans[d] = u.dist
也可以改成if (ans[d] < 0) ans[d] = u.dist
,因为被priority queue弹出,第一次出现的水量d
,他的dist
就是可以达到的最小值,因为之后所有弹出的node的dist
都大于当前的dist
if(i&& TEST(k,m-1)) ->>>if(i&& !TEST(k,m-1))
Uva12264 和12661
小dfs函数里面的else if字句应该把!dfs(v)放进判断条件,如果满足直接返回0吧。因为这个函数判断的应该是一条路上是否存在有向环,如果我错了希望有指出
现在通过这个网址进入,会重定向至uva首页http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=827
通过uva的Browse Problem里的这本书的那个文件夹进去也会重定向至首页
可以修复一下吗?
n<2^31, O(n) complexity會超時
參考http://www.algorithmist.com/index.php/UVa_10213 可用closed form計算 但答案太大要用大數解 (不確定是否有更好解法)
(內部交點可用c(n,4)計算 (任四點構成一交點,每一交點多一塊),邊數可用c(n,2)計算 (每多一條邊多一塊) 共 c(n,4)+c(n,2)+1 塊)
參考代碼:
import java.io.*;
import java.util.*;
import java.math.*;
class Main
{
public static void main (String args[])
{
Scanner cin=new Scanner(System.in);
int t,n;
t=cin.nextInt();
for(int i=0;i<t;i++){
n=cin.nextInt();
BigInteger ans;
ans=BigInteger.valueOf(n)
.multiply(BigInteger.valueOf(n-1))
.multiply(BigInteger.valueOf(n-2))
.multiply(BigInteger.valueOf(n-3))
.divide(BigInteger.valueOf(24))
.add(
BigInteger.valueOf(n)
.multiply(BigInteger.valueOf(n-1))
.divide(BigInteger.valueOf(2))
)
.add(BigInteger.valueOf(1));
System.out.println (ans);
}
}
}
#include<stdio.h>
int main()
{
int a[40],b[40],c[40],i;
int kase=0;
int x=0;
while(scanf("%d %d %d",&a[x],&b[x],&c[x])==3)
{
x++;
if(scanf("%d %d %d",&a[x],&b[x],&c[x])==3)
x++;
else
break;
}
int j=0;
for(;j<x;j++)
{ int print=1;//是否有解
for(i=10;i<=100;i++)
{
if(i%3==a[j]&&i%5==b[j]&&i%7==c[j])
{
printf("case %d:%d\n",++kase,i);
print=0;
break;
}
}
if(print) printf("case %d:No answer.\n",++kase);
}
return 0;
}
169页的示例代码如下:
void euler(int u){
for (int v=0; v<n; v++) if (g[u][v] && !vis[u][v]) {
vis[u][v] = vis[v][u] =1;
euler(v);
prinf("...");
}
}
这个代码貌似只有遍历的功能吧?
如题,如何解决?
请问第二章的代码在哪里?
Uva原题要求严格从左到右再从右到左。并且给出的状态定义和代码也不会考虑图(a)?
例题6-10在Uva上交会RE,init()函数最后要加上return true;才能过
范例代码中,为了计算s1(只有一个老师能授课的课程集合)和s2(至少有两个老师能授课的课程集合),作者引入了s0。但其实不需要s0。
对于雇佣第i名老师,下一轮次的dp改为:
dp(i+1, s1^st[i], s2|(s1&st[i])
即可。
2是镜像,但您的程序回答
22 -- is a regular palindrome.
22 should is a regular palindrome.
5.1.3 字符串 104页 提示5-5上方
原文是:
例如,C++的cin/cout可以直接读写string类型,却不能读写字符数组;string类型还可以像整数那样“相加”……
而在下面那份“输出每行所有整数之和”的代码中,有这样几行代码:
while(getline(cin,line)) {
int sum = 0, x;
stringstream ss(line);
while(ss >> x) sum += x;
cout << sum << "\n";
}
注意这里:
cout << sum << "\n";
"\n"本身就是一个“只有一个字符”的字符数组,而能写到cout里。这不与上面“不能读写字符数组”矛盾吗?仔细查一查iostream的资料,可以发现它们可以读写char*或者 char[]
建议作者改一下。。。
刘老师,您好!请问第7章egypt代码里的better函数是不是没有必要?我的理解是一旦找到解,dfs就会终止,搜索的顺序保证了是字典序最小?
In line 56
if(d[0][1] > d[0][0] && f[1][0]) unique = true;
should be
if(d[0][1] > d[0][0] && f[0][1]) unique = true;
Although the code is Accepted, I think it will meet problem in some cases.
In line 44 and 45
if(d[u][0] > INF) d[u][0] = INF; // avoid overflow!
if(d[u][1] > INF) d[u][1] = INF;
and line 51
d[u][2] = min(d[u][2], d[u][1] - d[v][2] + d[v][0]); // neither u or father is server
If d[u][1]
and d[v][2]
are set to INF. d[u][1] - d[v][2]
will be a zero, it’s obviously meaningless.
Although it is accepted, it will make mistakes in some test cases. Like
7
1 5
4 1
3 5
2 3
6 5
7 2
-1
the correct ans is 3, but the code get 1.
If we change it to this
if(d[v][0] > INF) d[v][0] = INF;
if(d[v][1] > INF) d[v][1] = INF * 2; // avoid INF - INF = 0
It will be all right.
不知道是不是题目更新了,总之“邮票组合中邮票的张数应最多”应改为“...应最少(由于这样可以节省印刷费用)”
// UVa514 Rails
// Rujia Liu
using namespace std;
const int MAXN = 1000 + 10;
int n, target[MAXN];
int main() {
scanf("%d", &n);
while(n) {
stack s;
int A = 1, B = 1;
scanf("%d",&target[1]);
if(!target[1]){
scanf("%d",&n);
continue;
}
for(int i = 2; i <= n; i++)
scanf("%d", &target[i]);
int ok = 1;
while(B <= n) {
if(A == target[B]){ A++; B++; }
else if(!s.empty() && s.top() == target[B]){ s.pop(); B++; }
else if(A <= n) s.push(A++);
else { ok = 0; break; }
}
printf("%s\n", ok ? "Yes" : "No");
}
return 0;
}
书上关于消耗能量的翻译,其中第三四行,书上原话是“如果这个脚上个时间单位移动到相邻箭头,则消耗5单位能量”,指的移动是上个时间单位里的移动,但是原题中的意思是“上个单位用了这个脚,现在这个时间单位的移动相邻,则消耗5能量”。
在程序中是按照原题的意思的,只是翻译有误。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.