西安电子科技大学计算机学院ACMICPC程序设计概述.ppt_第1页
西安电子科技大学计算机学院ACMICPC程序设计概述.ppt_第2页
西安电子科技大学计算机学院ACMICPC程序设计概述.ppt_第3页
西安电子科技大学计算机学院ACMICPC程序设计概述.ppt_第4页
西安电子科技大学计算机学院ACMICPC程序设计概述.ppt_第5页
已阅读5页,还剩49页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

西安电子科技大学计算机学院,ACM/ICPC程序设计概述,计算机学院 张淑平,西安电子科技大学计算机学院,Agenda,ACM/ICPC简介 题目示例 相关知识 相关网络资源,ACM/ICPC简介,西安电子科技大学计算机学院,ACM/ICPC简介,由国际计算机界历史最悠久、最具权威性的组织ACM学会(Association for Computer Machinery)主办。目前,ACM国际大学生程序设计大赛已经成为参赛选手展示计算机才华的广阔舞台,是著名大学计算机教育成果的直接体现,也是信息企业与世界顶尖计算机人才对话的最好机会,因此,该赛事已逐步演变成一项全球高校间计算机学科实力的竞赛。,西安电子科技大学计算机学院,ACM/ICPC简介,ACM/ICPC竞赛题目难度大,更强调算法的效率,竞赛时3人合作,共用一台计算机,非常注重团队协作精神;很多题目没有成熟的算法,旨在考验参赛队员的创新能力;比赛现场完全封闭,现场提交程序、现场评判,能客观、公正地展现参赛学生的真实水平。,西安电子科技大学计算机学院,ACM/ICPC简介,西安电子科技大学计算机学院,ACM/ICPC简介,西安电子科技大学计算机学院,ACM/ICPC简介,常用环境 C+:gcc/g+、Kylix Java:jdk、eclipse 重点:语言的标准库,西安电子科技大学计算机学院,Agenda,ACM/ICPC简介 题目示例 相关知识 相关网络资源,题目示例,西安电子科技大学计算机学院,Machine Schedule,Problem G: Machine Schedule (input file: machine.in) As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints that must be satisfied and the type of schedule desired. Here we consider a 2-machine scheduling problem. There are two machines A and B. Machine A has n kinds of working modes, which is called mode_0, mode_1, , mode_n-1, likewise machine B has m kinds of working modes, mode_0, mode_1, , mode_m-1. At the beginning they are both work at mode_0.,西安电子科技大学计算机学院,Machine Schedule(cont.),For k jobs given, each of them can be processed in either one of the two machines in particular mode. For example, job 0 can either be processed in machine A at mode_3 or in machine B at mode_4, job 1 can either be processed in machine A at mode_2 or in machine B at mode_4, and so on. Thus, for job i, the constraint can be represent as a triple (i, x, y), which means it can be processed either in machine A at mode_x, or in machine B at mode_y. Obviously, to accomplish all the jobs, we need to change the machines working mode from time to time, but unfortunately, the machines working mode can only be changed by restarting it manually. By changing the sequence of the jobs and assigning each job to a suitable machine, please write a program to minimize the times of restarting machines.,西安电子科技大学计算机学院,Machine Schedule(cont.),Input The input file for this program consists of several configurations. The first line of one configuration contains three positive integers: n, m (n, m 100) and k (k 1000). The following k lines give the constrains of the k jobs, each line is a triple: i, x, y. The input will be terminated by a line containing a single zero. Output The output should be one integer per line, which means the minimal times of restarting machine.,西安电子科技大学计算机学院,A+B problem,using C+: #include int main () int a,b; while(cin a b) cout a+b endl; ,西安电子科技大学计算机学院,典型题例:最大子段和,问题:给定n个整数(可能但不全为负)a1,a2,an, 求i,j,使ai到aj的和最大。 例如:6个数:(-2,11,-4,13,-5,-2) 最大和记做ms(p,q),表示ap到aq这一段的最大和。显然ms(1,6)=20,i=2, j=4。,西安电子科技大学计算机学院,最大子段和解法,最简单的是3重循环:2重遍历,1重求和。时间复杂度:O(n3) 改进方法之一:可以通过sum(i:j-1)+a(j)来求sum(i:j),代码见下页。时间复杂度:O(n2) 改进方法之二:先计算psub(k)=sum(1:k),然后sum(i,j)=psub(j)-psub(i-1),代码略。时间复杂度O(n2) 改进方法之三:动态规划法!,西安电子科技大学计算机学院,典型题例:最大子段和,int MaxSum(int n, int a, int ,西安电子科技大学计算机学院,最大子段和的DP解法,记bj=maxsum(i:j) , 1ij, 则maxbk,1 k n就是ms(1,n),即所求的全局最大和。,bj=max( bj-1+aj, aj ), 1=j=n,根据上式容易设计出最大子段和的DP算法,复杂度仅为O(n) ,详见代码。,西安电子科技大学计算机学院,最大子段和的DP解法,int MaxSum(int n, int a) int sum = 0, b = 0; for(int i=1; i 0 ) b += ai; else b = ai; if ( b sum ) sum = b; return sum; ,西安电子科技大学计算机学院,Agenda,ACM/ICPC简介 题目示例 相关知识 相关网络资源,相关知识,西安电子科技大学计算机学院,相关知识,基本数据结构 线性结构 数组 链表 栈和队列 Hash表 串 非线性结构 树(堆、二叉树等) 图,西安电子科技大学计算机学院,相关知识,基本算法设计策略 分治法 贪心法 动态规划 回溯法 分枝限界法 时间、空间复杂度分析方法,西安电子科技大学计算机学院,相关知识,图论相关知识 组合数学知识 计算几何知识 数论知识 程序设计语言 其它(博弈、运筹学),西安电子科技大学计算机学院,Beginners Guide,根据自己的实际情况补充C+(类的基本概念、语法、开发环境的基本使用、STL)、数据结构的基本知识。 听讲座:基本数据结构、基本算法(分治、贪心、DP、搜索)。 结合讲座内容做一些题目。 看书实用算法的分析与程序设计。 做usaco上的step by step。 参考oibh上入门FAQ,西安电子科技大学计算机学院,Agenda,ACM/ICPC简介 题目示例 相关知识 相关网络资源,相关网络资源,西安电子科技大学计算机学院,相关网络资源,UsacoGate:/usacogate UsacoContest:/contestgate USACO:/ UsacoGate 是全美计算机奥林匹克竞赛(USACO)的一个训练网站,要参加 USACO 就必须参加 UsacoGate 的训练。每年都有 USACO Spring,Fall,Winter,以及 WinterCamp 的全美竞赛,并且提供网络竞赛。 UsacoGate 的难度由浅入深,分了若干个专题,每个专题都有一篇讲座以及道题目供练习(提供在线即时评测系统)。个人认为,上面的有些题目比较经典,也有些题目不好,总的来说还是值得已经熟悉程序设计语言但是对算法和数据结构不太了解的学生去做的。每道题目后面都有解答,附有 C+ 程序,建议初学者多看看别人的解答和程序。 /usaco/default.asp 上有USACO上的题目和Text的翻译。,西安电子科技大学计算机学院,相关网络资源,/ 浙大 /JudgeOnline 北大 比赛官方网址,西安电子科技大学计算机学院,ACM/ICPC程序中的输入输出,西安电子科技大学计算机学院,输入输出的数据类型,整数 实数 字符 字符串,特别强调数据的格式,西安电子科技大学计算机学院,C语言的输入输出函数,标准I/O设备的数据输入输出函数 字符输入、输出函数 getchar( )、putchar( ) 格式输入、输出函数 scanf( )、printf( ) 字符串输入、输出函数 gets( )、puts( ),西安电子科技大学计算机学院,C语言的文件输入输出函数,文件数据的输入输出 打开、关闭文件 fopen( )、fclose() 判断文件是否结束 feof() 字符输入、输出函数 fgetc( )、fputc( )、getc( )、putc( ) 格式输入、输出函数 fscanf( )、fprintf( ) 字符串输入、输出函数 fgets( )、fputs( ),西安电子科技大学计算机学院,A+B problem,using C: #include int main() int a,b; while(scanf(“%d %d“, ,还可写成: scanf (“%d %d“,&a, &b) = 2,西安电子科技大学计算机学院,A+B problem(文件输入输出),using C: #include FILE *fin,*fout; int main() int a,b; fin=fopen(“input.in”,”r”); fout=fopen(“output.out”,”w”); while(fscanf(fin,“%d %d“, ,写成: fin=stdin; fout=stdout; 将等价于屏幕输入输出,西安电子科技大学计算机学院,C+的标准输出流,标准输出流对象cout 字符、整数、实数、字符串等的输出都用cout cout 需要输出的数据 cout.write() /按照指定长度输出字符串 cout.put() /输出一个字符 输出格式控制,西安电子科技大学计算机学院,C+的标准输入流,标准输入流对象cin 字符、整数、实数、字符串等的输入都用cin cin 变量名 cin.get() cin.getline(),西安电子科技大学计算机学院,A+B problem,using C+: #include int main () int a,b; while(cin a b) cout a+b endl; ,西安电子科技大学计算机学院,C+的文件输入输出流,文件流 输入文件流ifstream、输出文件流ofstream 、输入/输出文件流fstream 创建并打开文件流对象 文件的读写,西安电子科技大学计算机学院,A+B problem(文件输入输出),using C+: #include ifstream fin(“input.in”); ofstream fout(“output.out”); int main() int a,b; while(fin a b) fout a+b endl; ,西安电子科技大学计算机学院,C的输入输出 vs C+的输入输出,C: double a,b; scanf (“%lf %lf”,C+: double a,b; cinab;,西安电子科技大学计算机学院,例:C输出实数,输出,保留4位小数: C: double a; printf (“%.4lfn”,a);,西安电子科技大学计算机学院,例:C+输出实数,C+: #include #include int main() double a = 3.1415926535897932; coutaendl; cout.precision (7); coutaendl; cout.setf (ios:fixed); cout.precision (8); coutaendl; coutsetiosflags (ios:fixed)setprecision (8)aendl; return 0; ,西安电子科技大学计算机学院,例:C+输出实数,C+: #include #include #include int main() double a = 3.1415926535, b = acos ( -1); cout.setf ( ios:fixed); cout.precision ( 15); couta,bendl; if ( a=b) coutbendl; else coutaendl; if ( fabs ( a-b) 1e-7) coutbendl; else coutaendl; return 0; ,西安电子科技大学计算机学院,例:C输入输出字符,C: #include int main () char a; FILE * fin = fopen ( “input.txt“,“r“); while ( fscanf ( fin,“%c“, ,input.txt: 1+2 = 3 ; 4 +5= 9,西安电子科技大学计算机学院,例:C+输入输出字符,C+: #include #include int main () char a; ifstream fin ( “input.txt“); while ( fina) cout(int) a,aendl; return 0; ,input.txt: 1+2 = 3 ; 4 +5= 9,西安电子科技大学计算机学院,例:C+输入输出字符,C+: #include #include int main () char a; ifstream fin ( “input.txt“); while ( fin.get (a) cout(int)a,aendl; return 0; ,input.txt: 1+2 = 3 ; 4 +5= 9,西安电子科技大学计算机学院,例:C输入输出字符串,读写字符串(串以空格、回车分隔) #include int main () char a100; while ( scanf(“%s“,a) = 1) printf ( “%sn“,a); return 0; ,西安电子科技大学计算机学院,例:C输入输出字符串(续),读写字符串(串中有空格,串以回车分隔) #include int main () char a100; while ( gets (a) ) printf ( “%sn“,a); return 0; ,西安电子科技大学计算机学院,例:C+输入输出字符串,读写字符串(串以空格、回车分隔) #include int main () char a100; while ( cina) coutaendl; return 0; ,西安电子科技大学计算机学院,例:C+读入一行字符串,#include #include #include int main () int b=0; std:string s; ifstream fin( “input.txt“); while ( getline (fin, s) cout+b:sendl; return 0; ,input.txt: 1+2 = 3 ; 4 +5= 9,西安电子科技大学计算机学院,例:C从

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论