ACM题目及答案

合集下载

大学ACM考试题目及作业答案整理

大学ACM考试题目及作业答案整理

ACM作业与答案整理1、平面分割方法:设有n条封闭曲线画在平面上,而任何两条封闭曲线恰好相交于两点,且任何三条封闭曲线不相交于同一点,问这些封闭曲线把平面分割成的区域个数。

#include <iostream.h>int f(int n){if(n==1) return 2;else return f(n-1)+2*(n-1);}void main(){int n;while(1){cin>>n;cout<<f(n)<<endl;}}2、LELE的RPG难题:有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.编程全部的满足要求的涂法.#include<iostream.h>int f(int n){if(n==1) return 3;else if(n==2) return 6;else return f(n-1)+f(n-2)*2;}void main(){int n;while(1){cin>>n;cout<<f(n)<<endl;}}3、北大ACM(1942)Paths on a GridTime Limit: 1000MS Memory Limit: 30000K DescriptionImagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time he's explaining that (a+b)2=a2+2ab+b2). So you decide to waste your time with drawing modern art instead.Fortunately you have a piece of squared paper and you choose a rectangle of size n*m on the paper. Let's call this rectangle together with the lines it contains a grid. Starting at the lower left corner of the grid, you move your pencil to the upper right corner, taking care that it stays on the lines and moves only to the right or up. The result is shown on the left:Really a masterpiece, isn't it? Repeating the procedure one more time, you arrive with the picture shown on the right. Now you wonder: how many different works of art can you produce?InputThe input contains several testcases. Each is specified by two unsigned 32-bit integers n and m, denoting the size of the rectangle. As you can observe, the number of lines of the corresponding grid is one more in each dimension. Input is terminated by n=m=0.OutputFor each test case output on a line the number of different art works that can be generated using the procedure described above. That is, how many paths are there on a grid where each step of the path consists of moving one unit to the right orone unit up? You may safely assume that this number fits into a 32-bit unsigned integer.Sample Input5 41 10 0Sample Output1262#include<iostream>using namespace std;longlong f(long long m, long long n){if(n==0) return 1;else return f(m-1,n-1)*m/n;}int main(){longlongm,n;while(scanf("%I64d %I64d",&n,&m) &&n+m){printf("%I64d\n",f(m+n,min(m,n)));}return 0;}1、(并查集)若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。

ACM考试题

ACM考试题

ACM程序设计东北林业大学陈宇Lg_chenyu@第一讲算法原理和ACM入门(Introduction to ACM)我校的ACM在线评测系统**课件下载地址:*/kj/suanfa01.ppt预期赛事(今后每年)*3~4月,举行校内大赛(暨选拔赛)*4月,ACM全国邀请赛*5月,参加黑龙江省大学生程序设计大赛*6月,参加东北4省大学生程序设计大赛*10~11月,参加ACM/ICPC亚洲区比赛(至少参加4~5个赛区的比赛)*另外,每学期至少有三次月赛以及适当的练习赛第一部分算法概述*算法分析(Algorithm Analysis):对算法所需要的两种计算机资源——时间和空间进行估算;时间复杂性(Time Complexity)空间复杂性(Space Complexity)算法分析的目的:设计算法——设计出复杂性尽可能低的算法选择算法——在多种算法中选择其中复杂性最低者算法的描述语言:⑴自然语言优点:容易理解缺点:冗长、二义性使用方法:粗线条描述算法思想注意事项:避免写成自然段(2)流程图优点:流程直观缺点:缺少严密性、灵活性使用方法:描述简单算法注意事项:注意抽象层次⑶程序设计语言优点:能由计算机执行缺点:抽象性差,对语言要求高使用方法:算法需要验证注意事项:将算法写成子函数(4)伪代码——算法语言伪代码(Pseudocode):介于自然语言和程序设计语言之间的方法,它采用某一程序设计语言的基本语法,操作指令可以结合自然语言来设计。

优点:表达能力强,抽象性强,容易理解评价算法*评价算法的三条主要标准是:*(1) 算法实现所耗费的时间;*(2) 算法实现所所耗费的存储空间,其中*主要考虑辅助存储空间;*(3) 算法应易于理解,易于编码,易于调*试等等。

和算法执行时间相关的因素:1)问题中数据存储的数据结构2)算法采用的数学模型3)算法设计的策略4)问题的规模5)实现算法的程序设计语言6)编译算法产生的机器代码的质量7)计算机执行指令的速度算法效率的衡量方法*通常有两种衡量算法效率的方法:*1)事后统计法(有缺点,较少使用)*2)事前分析估算法*算法的时间效率是问题规模的函数。

ACM题库完整版

ACM题库完整版

#include<stdio.h> int type(int); char week[7][10]={"saturday","sunday","monday","tuesday","wednesday","thursday","friday"}; int year[2]={365,366}; int month[2][12]={31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31}; int main(void) { int days,dayofweek; int i=0,j=0; while(scanf("%d",&days)&&days!=-1) { dayofweek=days%7; for(i=2000;days>=year[type(i)];i++) days-=year[type(i)]; for(j=0;days>=month[type(i)][j];j++) days-=month[type(i)][j]; printf("%d-%02d-%02d%s\n",i,j+1,days+1,week[dayofweek]); } return 0; } int type(int m) { if(m%4!=0||(m%100==0&&m%400!=0)) return 0; else return 1; }
登山
1.题目描述 五一到了,NUIST-ACM队组织大家去登 山观光,队员们发现山上一个有N个景点, 并且决定按照顺序来浏览这些景点,即 每次所浏览景点的编号都要大于前一个 浏览景点的编号。同时队员们还有另一 个登山习惯,就是不连续浏览海拔相同 的两个景点,并且一旦开始下山,就不 再向上走了。队员们希望在满足上面条 件的同时,尽可能多的浏览景点,你能 帮他们找出最多可能浏览的景点数么? 2.输入 Line 1: N (2 <= N <= 1000) 景点数 Line 2: N个整数,每个景点的海拔 3.输出 最多能浏览的景点数 4.样例输入 8 186 186 150 200 160 130 197 220 5.样例输出 4

ACM题目、测试用例及参考答案汇编——一次ACM协会内部测试

ACM题目、测试用例及参考答案汇编——一次ACM协会内部测试

ACM题目、测试用例及参考答案汇编——一次ACM协会内部测试第一题:梦境是虚幻吗?时间限制:3000ms 内存限制:65535KB 难度:★★描述《盗梦空间》是一部精彩的影片,在这部电影里,Cobb等人可以进入梦境之中,梦境里的时间会比现实中的时间过得快得多,这里假设现实中的3分钟,在梦里就是1小时。

然而,Cobb他们利用强效镇静剂,可以从第一层梦境进入第二层梦境,甚至进入三层,四层梦境,每层梦境都会产生同样的时间加速效果。

那么现在给你Cobb在各层梦境中经历的时间,你能算出现实世界过了多长时间吗?比如,Cobb先在第一层梦境待了1个小时,又在第二层梦境里待了1天,之后,返回第一层梦境之后立刻返回了现实。

那么在现实世界里,其实过了396秒(6.6分钟)输入第一行输入一个整数T(0<=T<=100),表示测试数据的组数。

每组测试数据的第一行是一个数字M(3<=M<=100)随后的M行每行的开头是一个字符串,该字符串如果是"IN" 则Cobb向更深层的梦境出发了,如果是字符串"OUT"则表示Cobb从深层的梦回到了上一层。

如果是首字符串是"STAY"则表示Cobb在该层梦境中停留了一段时间,本行随后将是一个整数S表示在该层停留了S分钟(1<=S<=10000000)。

数据保证在现实世界中,时间过了整数秒。

输出对于每组测试数据,输出现实世界过的时间(以秒为单位)。

样例输入16INSTAY 60INSTAY 1440OUTOUT样例输出396测试输入106INSTAY 60INSTAY 1440OUTOUT6INININOUTOUTOUT7INININSTAY 0 OUTOUTOUT2INSTAY 203INSTAY 0 OUT3INSTAY 10 OUT4INSTAY 10 STAY 10 OUT5INSTAY 20 STAY 20 OUT STAY 120 10INSTAY 20 STAY 20 INSTAY 1440STAY 1440OUTSTAY 120OUTSTAY 11STAY 50测试输出39660306073209723000参考代码:#include<stdio.h>int main(){int n;char a[5];scanf("%d",&n);while(n--){int m,i,b=1,c,time=0;scanf("%d",&m);for(i=0;i<m;i++){scanf("%s",&a);if(a[0]=='I') b*=20;else if(a[0]=='S') {scanf("%d",&c);time+=c*60/b;} else if(a[0]=='O') b/=20;}printf("%d\n",time);}return 0;}第二题:独木舟过河时间限制:3000ms 内存限制:65535KB 难度:★★描述进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别。

acm竞赛试题及答案

acm竞赛试题及答案

acm竞赛试题及答案ACM(Association for Computing Machinery)竞赛是一项全球性计算机科学竞赛,旨在锻炼参赛者的问题解决能力和编程技巧。

每年都有数千名来自不同学校的学生参加这一挑战。

本文将提供一些最近的ACM竞赛试题以及相应的答案,帮助读者了解和学习竞赛题目的类型和解题思路。

1. 问题描述给定一个由N个整数组成的数组A,请编写一个程序,找出数组中两个不同元素的差的最小值。

2. 输入格式- 第一行包含一个整数N,表示数组A的长度。

- 第二行包含N个以空格分隔的整数,表示数组A中的元素。

3. 输出格式输出一个整数,表示数组中两个不同元素的差的最小值。

4. 示例输入:51 52 9 12输出:15. 解题思路该问题可以通过对数组进行排序,并比较相邻两个数的差值来求解。

首先,将数组A进行升序排序。

然后,遍历排序后的数组,依次计算相邻两个数的差值,并记录其中的最小值。

最后,返回这个最小差值即可。

6. 代码实现```pythondef min_difference(nums):nums.sort() # 对数组进行升序排序min_diff = float('inf') # 初始化最小差值为正无穷大for i in range(len(nums)-1):diff = abs(nums[i] - nums[i+1]) # 计算相邻两个数的差值min_diff = min(min_diff, diff) # 更新最小差值return min_diff# 输入处理N = int(input())A = list(map(int, input().split()))# 调用函数并输出结果result = min_difference(A)print(result)```7. 答案解析对给定的数组进行排序后,遍历数组计算相邻两个数的差值,并记录其中的最小值。

上述代码中,首先将数组A进行升序排序,然后使用一个变量`min_diff`来记录最小差值。

整理出ACM所有题目及答案

整理出ACM所有题目及答案

1000 A + B ProblemProblem DescriptionCalculate A + B .InputEach line will contain two integers A and B. Process to end of file.OutputFor each case, output A + B in one line.Sample Input1 1Sample Output2AuthorHDOJ代码:#include<stdio.h>int main(){int a , b;while( scanf ( "%d %d" ,& a,& b)!= EOF)printf( "%d\n" , a+b);}1001 Sum ProblemProblem DescriptionHey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.InputThe input will consist of a series of integers n, one integer per line.OutputFor each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.Sample Input1100Sample Output15050AuthorDOOM III解答:#include<stdio.h>main (){int n , i , sum;sum =0;while (( scanf ( "%d" ,& n)!=- 1)) {sum for =0;( i =0; i <= n; i ++)sum +=i ;printf( "%d\n\n" , sum);}}1002 A + B Problem IIProblem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. InputThe first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.OutputFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.Sample Input21 2Sample OutputCase 1:1+2=3Case 2:Author代码:#include <stdio.h>#include <string.h>int main (){char str1 [ 1001 ], str2 [ 1001 ];int t , i , len_str1 , len_str2 , len_max , num = 1 , k ; scanf ( "%d" , & t );getchar ();while ( t --){int a [ 1001 ] = { 0}, b [1001]={ 0}, c [1001]={ 0};scanf ( "%s" , str1 );len_str1 = strlen ( str1 );for ( i = 0 ; i <= len_str1 - 1;++ i )a [ i ] = str1 [ len_str1 - 1 - i ] - '0' ;scanf ( "%s" , str2 );len_str2 = strlen ( str2 );for ( i = 0 ; i <= len_str2 - 1;++ i )b [ i ] = str2 [ len_str2 - 1 - i ] - '0' ;if ( len_str1 > len_str2 )len_max = len_str1 ;elselen_max = len_str2 ;k = 0 ;for ( i = 0 ; i <= len_max - 1 ;++ i ){c [ i ] = ( a[ i ] + b [ i ] + k ) % 10 ;k = ( a[ i ] + b [ i ] + k ) / 10 ;}if ( k != 0 )c [ len_max ] = 1 ;printf ( "Case %d:\n" , num );num ++;printf ( "%s + %s = " , str1 , str2 );if ( c[ len_max ] == 1 )printf ( "1" );for ( i = len_max - 1 ; i >= 0 ;-- i ){printf ( "%d" , c [ i ]);}printf ( "\n" );if ( t >= 1 )printf ( "\n" );}return 0 ;}1005 Number Sequence Problem DescriptionA number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.Given A, B, and n, you are to calculate the value of f(n).InputThe input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.OutputFor each test case, print the value of f(n) on a single line.Sample Input1 1 312100 0 0Sample Output25AuthorCHEN, ShunbaoSourceRecommendJGShining代码:#include<stdio.h>int f [ 200 ];int main (){int a , b, n, i ;while ( scanf ( "%d%d%d" ,& a,& b,& n)&& a&&b&&n){if ( n>= 3){f [ 1]= 1; f [ 2]= 1;for ( i =3; i <= 200 ; i ++){f [ i ]=( a* f [ i - 1]+ b* f [ i - 2])% 7;if ( f [ i - 1]== 1&&f [ i ]== 1)break ;}i -= 2;n =n%i ;if ( n== 0)printf ( "%d\n" , f [ i ]);elseprintf ( "%d\n" , f [ n]);}elseprintf ( "1\n" );}return 0 ;}1008 ElevatorProblem DescriptionThe highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevatorup one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.InputThere are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.OutputPrint the total time on a single line for each test case.Sample Input1 23231Sample Output1741AuthorZHENG, JianqiangSourceRecommendJGShining代码:#include<stdio.h>int a [ 110 ];int main(){int while { sum , i , n;( scanf ( "%d" ,& n)&& n!= 0)forscanf ( i =1; i <= n; i ++)( "%d" ,& a[ i ]);sum a for=0;[ 0]= 0;( i =1; i <= n; i ++){ifsum ( a[ i ]> a[ i - 1])+=6*( a[ i ]- a[ i - 1]);elsesum +=4*( a[ i - 1]- a[ i ]);sum +=5;printf ( "%d\n" , sum);}return 0 ;}1009 FatMouse' TradeProblem DescriptionFatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.InputThe input consists of multiple test cases. Each test case begins with a line containing two non-negative integersM and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test caseis followed by two -1's. All integers are not greater than 1000.OutputFor each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.Sample Input53724352203251824151510-1-1Sample OutputAuthorCHEN, YueSourceRecommendJGShining代码:#include<stdio.h>#include<string.h>#define MAX 1000int main(){int i,j,m,n,temp;int J[MAX],F[MAX];double P[MAX];double sum,temp1;scanf("%d%d",&m,&n);while(m!=-1&&n!=-1){sum=0;memset(J,0,MAX*sizeof(int));memset(F,0,MAX*sizeof(int));memset(P,0,MAX*sizeof(double));for(i=0;i<n;i++){ scanf("%d%d",&J[i],&F[i]); P[i]=J[i]*1.0/((double)F[i]); }for(i=0;i<n;i++){for(j=i+1;j<n;j++){if(P[i]<P[j]){temp1=P[i];P[i]=P[j];P[j]=temp1;temp=J[i]; J[i]=J[j]; J[j]=temp; temp=F[i];F[i]=F[j]; F[j]=temp;} }}for(i=0;i<n;i++) { if(m<F[i]){ else{sum+=m/((double)F[i])*J[i];sum+=J[i];break;m-=F[i];} }}printf("%.3lf\n",sum); scanf("%d%d",&m,&n); }return 0; }1021 Fibonacci AgainProblem DescriptionThere are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).InputInput consists of a sequence of lines, each containing an integer n. (n < 1,000,000).OutputPrint the word "yes" if 3 divide evenly into F(n). Print the word "no" if not.Sample Input0 1 2 3 4 5Sample Outputno no yes no no noAuthorLeojayRecommendJGShining#include<stdio.h> int main () { long while if printfn ;( scanf ( "%ld" ,& n) !=( n%8==2 || n %8==6) ( "yes\n" ); EOF ) elseprintf ( "no\n");return0 ;}1089 A+B for Input-Output Practice (I)Problem DescriptionYour task is to Calculate a + b.Too easy?! Of course! I specially designed the problem for acm beginners.You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input151020Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main (){int a , b;while( scanf ( "%d%d" ,& a,& b)!=EOF)printf( "%d\n" , a+b);}1090 A+B for Input-Output Practice (II)Problem DescriptionYour task is to Calculate a + b.InputInput contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input2151020Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>#define M 1000void main (){int a , b, n, j [ M], i ;//printf("please input n:\n");scanf( "%d" ,& n);for( i =0; i <n; i ++){scanf( "%d%d" ,& a,& b);//printf("%d %d",a,b);j[ i ]= a+b;}i=0;while( i <n){printf( "%d" , j [ i ]);i++;printf( "\n" );}}1091 A+B for Input-Output Practice(III) Problem DescriptionYour task is to Calculate a + b.InputInput contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line.A test case containing 0 0 terminates the input and this test case is not to be processed.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line ofoutput for each line in input.Sample Input15102000Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main (){int a , b;scanf( "%d %d" ,& a,& b);while(!( a== 0&&b==0)){printf( "%d\n" , a+b);scanf( "%d %d" ,& a,& b);}}1092 A+B for Input-Output Practice(IV) Problem DescriptionYour task is to Calculate the sum of some integers.InputInput contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input.。

acm大学生程序设计试题

acm大学生程序设计试题

acm大学生程序设计试题题目一:最大公约数(GCD)题目描述:给定两个正整数,求它们的最大公约数(GCD)。

输入两个正整数a和b(1 <= a, b <= 10^9),求它们的最大公约数。

输入格式:两个正整数,以空格分隔。

输出格式:输出一个整数,表示输入两个正整数的最大公约数。

示例:输入:14 21输出:7思路和分析:最大公约数(GCD)可以使用欧几里得算法来求解,即辗转相除法。

具体的步骤如下:1. 用较大的数除以较小的数,将得到的余数作为新的较大数。

2. 再用新的较大数除以较小数,将得到的余数作为新的较大数。

3. 如此重复,直到两个数可以整除,此时较小的数就是最大公约数。

代码实现:```cpp#include <iostream>using namespace std;int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);}int main() {int a, b;cin >> a >> b;int result = gcd(a, b);cout << result << endl;return 0;}```题目二:字符串反转题目描述:给定一个字符串,要求将其反转并输出。

输入一个字符串s(1 <= |s| <= 1000),输出该字符串的反转结果。

输入格式:一个字符串s,只包含大小写字母和数字。

输出格式:一个字符串,表示输入字符串的反转结果。

示例:输入:HelloWorld123输出:321dlroWolleH思路和分析:字符串反转可以使用双指针的方法来实现。

初始时,左指针指向字符串的开头,右指针指向字符串的末尾,然后交换左右指针所指向的字符,并向中间移动,直到左指针不小于右指针。

代码实现:```cpp#include <iostream>using namespace std;string reverseString(string s) {int left = 0, right = s.length() - 1; while (left < right) {swap(s[left], s[right]);left++;right--;}return s;}int main() {string s;cin >> s;string result = reverseString(s); cout << result << endl;return 0;}```题目三:字符串匹配题目描述:给定一个字符串s和一个模式串p,判断s中是否存在与p相匹配的子串。

2019年南华大学ACM校赛题解

2019年南华大学ACM校赛题解

2019年南华大学ACM校赛题解题解:A题题目意思很明确,求一个三项式展开式中的系数是多少。

首先需要了解最基本的公式没学过三项式展开怎么办因为我们学过二项式展开公式,三项式自然就是多展开一次可以了,接下来就是求的问题了由于此题数据量比较小,初略一看,直接暴力就可以写出,怎么暴力?求n的阶乘,放心循环就可以了(才1000的数据量),再做除法,取个模。

这样写有个致命的错误,就是除法的取余的问题,除法不满足拆开分别取余,再相除后取余的,如果是对质数取余,可以考虑下费小马+快速幂。

高级解法:杨辉三角,找找杨辉三角的层数和对应位置就知道,正好与之对应,中间过程取余,因为递推是加法,所以不影响最终的结果。

此题只要注意对中间数不断取余,就不会有太大问题。

B题暴力呗,签到题啊。

会写判质数就可以了。

C题并查集,也没有什么别的东西。

不过数据不是很强大。

不用压缩路径好像也能过。

统计一下一个集合有多少个元素减一就好了。

时间复杂度:O(m)+O(n)?D题DP吧。

先可以把删除长度所的到的价值用O(n^2)预处理一下。

得到删除长度所得到的最大价值。

设x[i]为删除长度i所得到的最大价值,等于max(x[j]+x[i-j],x[i])(0<=j<=i)(虽然不这么做也可以得到答案)。

考虑题目所给的操作,我们从中删除一段,再把前后拼接起来,如何设置状态?先看一个例子:11111011110000然后我们删除0后变为1111111110000答案等于单独的0加上{11111}11110000这样我们可以设置一个前缀,这样我们就可以表示任何地方删除的状态了,答案不会变。

我们可以压缩一下相同的元素。

(因为我们提前预处理过最大价值)设dp[l][r][v]表示l+1到r加上前缀的v的最大答案。

压缩后得到数组z。

我们来考虑一下状态的转移:1、我们直接删除前缀v,dp[l][r][v] = x[v] + dp[l+1][r][z[l+1]],2、然后我们删中间任意一段,dp[l][r][v]= max ( dp[l][r][v] ,dp[l+1][i][z[l+1]] + dp[i][r][v+z[i] ) (必须满足条件(l%2)==(i%2)因为只有这样删除才能把前缀合在一起。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Qwertyuiopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmqwertyuiopasdfgh jklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzxc vbnmqwertyuiopasdfghjklzxcvbnmqw ertyuiopasdfghjklzxcvbnmqwertyuiopa sdfghjklzxcvbnmqwertyuiopasdfghjklz xcvbnmqwertyuiopasdfghjklzxcvbnmq wertyuiopasdfghjklzxcvbnmqwertyuio zxcvbnmqwertyuiopasdfghjklzxcvbnm qwertyuiopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmrtyuiopasdfghjklz xcvbnmqwertyuiopasdfghjklzxcvbnmq wertyuiopasdfghjklzxcvbnmqwertyuio pasdfghjklzxcvbnmqwertyuiopasdfghj Sincisco ACM 答案 2010/11/10 Sincisco 小组成员排球队员站位问题i【题目】排球队员站位问题┏━━━━━━━━┓图为排球场的平面图,其中一、二、三、四、五、六为位置编号,┃┃二、三、四号位置为前排,一、六、五号位为后排。

某队比赛时,┃┃一、四号位放主攻手,二、五号位放二传手,三、六号位放副攻┠──┬──┬──┨手。

队员所穿球衣分别为1,2,3,4,5,6号,但每个队┃ 四│ 三│ 二┃员的球衣都与他们的站位号不同。

已知1号、6号队员不在后排,┠──┼──┼──┨2号、3号队员不是二传手,3号、4号队员不在同一排,5号、┃ 五│ 六│ 一┃6号队员不是副攻手。

┗━━┷━━┷━━┛编程求每个队员的站位情况。

【算法分析】本题可用一般的穷举法得出答案。

也可用回溯法。

#include <cstdlib>#include <iostream>using namespace std;int counter=0;void display(int place[]){for(int i=1;i<=6;i++){cout<<place[i]<<"\t";if(i%3==0)cout<<"\n";}cout<<endl;}bool judge(int mark,int place[]){switch(mark){case 1:return true;case 2:return place[2]!=5&&place[2]!=6;case 3:return place[3]!=2&&place[3]!=3;case 4:return place[4]!=1&&place[4]!=2&&place[4]!=3&&place[4]!=6; case 5:return place[5]!=1&&place[5]!=5&&place[5]!=6;case 6:return place[6]!=1&&place[6]!=6; }}bool others(int place[]){int m=1,n=1;for(int i=0;i<=6;i++){if(place[i]==3)m=i-1;if(place[i]==4)n=i-1;}return m/3!=n/3;}void backtrace(int mark,int place[],int people[]){for(int i=1;i<=6;i++){if(people[i]!=0){place[mark]=people[i];if(judge(mark,place)){if(mark==6&&others(place)){counter++;display(place);}else{people[i]=0;backtrace(mark+1,place,people);people[i]=i;}}}}}int main(int argc, char *argv[]){int *place=new int[7];int *people=new int[7];for(int i=1;i<=6;i++)people[i]=i;backtrace(1,place,people);cout<<"The total methods is "<<counter<<endl;system("PAUSE");return EXIT_SUCCESS;}数字布置问题【题目】把1-8这8个数放入下图8个格中,要求相邻的格(横,竖,对角线)上填的数不连续.┌─┐│①│┌─┼─┼─┐│②│③│④│├─┼─┼─┤│⑤│⑥│⑦│└─┼─┼─┘│⑧│└─┘#include <cstdlib>#include <iostream>using namespace std;void display(int field[],int mark){for(int i=1;i<=mark;i++)cout<<field[i]<<"\t";cout<<endl;}bool continues(int m,int n){if((m-n==-1)||(m-n==1)){return true;}else{return false;}}bool judge(int mark,int field[]){switch(mark){case 1:return true;case 2:return true;case 3:if(continues(field[3],field[1])||continues(field[3],field[2]))return false;elsereturn true;case 4:if(continues(field[3],field[4]))return false;elsereturn true;case 5:if(continues(field[5],field[2]))return false;elsereturn true;case 6:if(continues(field[3],field[6])||continues(field[5],field[6]))return false;elsereturn true;case 7:if(continues(field[7],field[4])||continues(field[7],field[6]))return false;elsereturn true;case 8:if(continues(field[8],field[6]))return false;elsereturn true;}}void backtrace(int num[],int field[],int mark){for(int i=1;i<=8;i++){if(num[i]!=0){field[mark]=num[i];if(judge(mark,field)){if(mark==8)display(field,mark);else{num[i]=0;backtrace(num,field,mark+1);num[i]=i;}}}}}int main(int argc, char *argv[]){int *num;int *field;num=new int[9];field=new int[9];for(int i=0;i<9;i++){num[i]=i;}backtrace(num,field,1);system("PAUSE");return EXIT_SUCCESS;}改错题下列程序的功能是:一只甲虫在一个迷宫(一个10×10的矩阵表示)中移动,迷宫中有若干个柱子(在矩阵中表示为值为1的单元)。

甲虫从迷宫中的某个位置出发,按照预先设置的指令在迷宫中行进:指令是一串由1~4组成的数字,分别代表行进的4个方向:1代表向上,2代表向右,3代表向下,4代表向左。

当按照移动指令移动后的位置是一个柱子或超出了迷宫的范围则忽略该步指令。

最终程序将显示指令序列结束后的甲虫所处的位置。

修改后的结果:#include <iostream.h>int m[5][5]={1,0,0,1,0,0,1,0,1,1,0,0,0,0,0,1,0,1,0,0,0,1,1,0,0}; //迷宫,1-柱子int isPole(int x,int y){if(m[x][y]==1)return 1;elsereturn 0;}void move(int d[],int x[2],int n){int nx, ny;for(int i=0; i<n ; i++){switch(d[i]){case 1:ny = x[1]-1;break;case 2:nx = x[0]+1;break;case 3:ny = x[1]+1;break;case 4:nx = x[0]-1;break;default:break;}if(isPole(nx,ny)||nx<0||nx>4||ny<0||ny>4)continue;x[0]=nx; x[1]=ny;}}void main(){int x[2];int d[]={2, 2, 3, 3, 1, 3, 3, 4, 4, 1, 2, 3, 2, 2, 4}; //指令集cout<<"输入初始位置: ";cin>>x[0]>>x[1];move(d,x,sizeof(d[0])/sizeof(d));cout<<"The final position is: "<<x[0]<<", "<<x[1]<<endl;}2、给定一个整数n,求出所有连续的且和为n正整数。

相关文档
最新文档