算法

Brackets Sequence
题目描述
Let us define a regular brackets sequence in the following way:

  1. Empty sequence is a regular sequence.
  2. If S is a regular sequence, then (S) and [S] are both regular sequences.
  3. If A and B are regular sequences, then AB is a regular sequence.

For example, all of the following sequences of characters are regular brackets sequences:

(), [], (()), ([]), ()[], ()[()]

And all of the following character sequences are not:

(, [, ), )(, ([)], ([(]

Some sequence of characters ‘(’, ‘)’, ‘[’, and ‘]’ is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 … an is called a subsequence of the string b1 b2 … bm, if there exist such indices 1 = i1 < i2 < … < in = m, that aj = bij for all 1 <= j< = n.
输入
The input file contains at most 100 brackets (characters ‘(’, ‘)’, ‘[’ and ‘]’) that are situated on a single line without any other characters among them.
输出
Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.
样例输入
([(]
样例输出
()[()]

#include <bits/stdc++.h>
using namespace std;
char s[110];
int dp[110][110]={0},pos[110][110];void print(int x,int y)
{if(x>y) return;if(x==y){if(s[x]=='('||s[y]==')')printf("()");elseprintf("[]");}else if(pos[x][y]==-1){printf("%c",s[x]);print(x+1,y-1);printf("%c",s[y]);}else{print(x,pos[x][y]);print(pos[x][y]+1,y);}
}
int main()
{cin>>s;int len=strlen(s);for(int i=0;i<len;i++)dp[i][i]=1;for(int i=1;i<len;i++){for(int j=0;j<len-i;j++){int k=i+j;dp[j][k]=0x7fffffff;if((s[j]=='('&&s[k]==')')||(s[j]=='['&&s[k]==']')){dp[j][k]=dp[j+1][k-1];pos[j][k]=-1;}for(int mid=j;mid<k;mid++){if(dp[j][k]>dp[j][mid]+dp[mid+1][k]){dp[j][k]=dp[j][mid]+dp[mid+1][k];pos[j][k]=mid;} }}}print(0,len-1);return 0;
}

Dollars
简介:新西兰货币

#include <bits/stdc++.h>
using namespace std;
int  main()
{int dollers[12]={1,2,4,10,20,40,100,200,400,1000,2000};//定义为int类型 long long int sum[6000]={1};//longlong类型   sum[i]初值都为1也可以 //sum[0]=1;//注意赋初值 for(int i=0;i<11;i++){for(int j=dollers[i];j<=6000;j++){sum[j]+=sum[j-dollers[i]];}}double a;//double类型就可以 while(~scanf("%lf",&a)&&a){int aa=(int)(a*1.0*100+0.5);printf("%6.2lf%17lld\n",a,sum[aa/5]);}return 0;
}

输出:

Longest Match
主要思路是对字符串的处理
构造结构体也很重要

#include <bits/stdc++.h>
#include <string.h>
using namespace std;
int dp[1010][1010];
struct node
{int num;string word[1010];
};
void spa(string s,node &t)//注意&t
{int len=s.length();t.num=1;//直接赋值不需定义for(int i=0;i<len;i++){if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||(s[i]>='0'&&s[i]<='9'))t.word[t.num]+=s[i];elset.num++;}int n=0;for(int i=1;i<=t.num;i++) {if(!t.word[i].empty())t.word[++n]=t.word[i];}t.num=n;
}
int  main()
{int pp=1;while(!cin.eof()){string x,y;node t1,t2;getline(cin,x);spa(x,t1);getline(cin,y);spa(y,t2);printf("%2d. ",pp++);if(x.empty()||y.empty()){printf("Blank!\n");continue;}memset(dp,0,sizeof(dp));for(int i=1;i<=t1.num;i++){for(int j=1;j<=t2.num;j++){if(t1.word[i]==t2.word[j])dp[i][j]=max(dp[i-1][j-1]+1,dp[i][j]);elsedp[i][j]=max(dp[i-1][j],dp[i][j-1]);}}printf("Length of longest match: %d\n",dp[t1.num][t2.num]);}return 0;
}

Wavio Sequence
样例输入
10
1 2 3 4 5 4 3 2 1 10
19
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1
5
1 2 3 4 5
样例输出
9
9
1
主要思路是使用了dp数组,每当遇到非递增元素寻找其在dp中的位置,记录其下标,其下标就是其可形成的递增序列长度

#include <bits/stdc++.h>
using namespace std;
int len,a[10010],ans1[10010],ans2[10010],dp[10010];
int find(int k)
{int mid=0,l=1,r=len;while(l<r){mid=(l+r)/2;if(dp[mid]<k)l=mid+1;elser=mid;}return l;
}
int main()
{int n;dp[0]=-1;while(~scanf("%d",&n)){for(int i=1;i<=n;i++)scanf("%d",&a[i]);len=0;int k;for(int i=1;i<=n;i++){if(a[i]>dp[len]){dp[++len]=a[i];ans1[i]=len;}else{k=find(a[i]);dp[k]=a[i];ans1[i]=k;}}len=0;for(int i=n;i>0;i--){if(a[i]>dp[len]){dp[++len]=a[i];ans2[i]=len;}else{k=find(a[i]);dp[k]=a[i];ans2[i]=k;}}int ans=1;for(int i=1;i<=n;i++)ans=max(ans,min(ans1[i],ans2[i])*2-1);printf("%d\n",ans);}}

FatMouse’s Trade
和homework有异曲同工之处
样例输入
4 2
4 7
1 3
5 5
4 8
3 8
1 2
2 5
2 4
-1 -1
样例输出
2.286
2.500

#include <bits/stdc++.h>
using namespace std;
struct node
{double v,t;double arg;
}a[1005];
bool cmp(node x,node y)
{return x.arg>y.arg;
}
int main()
{int n;double m;while(~scanf("%lf%d",&m,&n)){double sum=0;if(m==-1&&n==-1)break;for(int i=0;i<n;i++){scanf("%lf%lf",&a[i].v,&a[i].t);a[i].arg=a[i].v/a[i].t;}sort(a,a+n,cmp);int i;for(i=0;i<n;i++){if(m>=a[i].t){m-=a[i].t;sum+=a[i].v;}else{sum+=m*1.0*a[i].arg;break;}   }printf("%.3lf\n",sum);}return 0;
}

Schedule
题目描述
There are N schedules, the i-th schedule has start time si and end time ei (1 <= i <= N). There are some machines. Each two overlapping schedules cannot be performed in the same machine. For each machine the working time is defined as the difference between timeend and timestart , where time_{end} is time to turn off the machine and timestart is time to turn on the machine. We assume that the machine cannot be turned off between the timestart and the timeend.
Print the minimum number K of the machines for performing all schedules, and when only uses K machines, print the minimum sum of all working times.
输入
The first line contains an integer T (1 <= T <= 100), the number of test cases. Each case begins with a line containing one integer N (0 < N <= 100000). Each of the next N lines contains two integers si and ei (0<=si<ei<=1e9).

输出
For each test case, print the minimum possible number of machines and the minimum sum of all working times
样例输入
1
3
1 3
4 6
2 5
样例输出
2 8

#include <bits/stdc++.h>
using namespace std;
struct node
{int st,flag;
}work[100005*2];
bool cmp(node x,node y)
{if(x.st!=y.st) return x.st<y.st;else return x.flag<y.flag;
}
int main()
{int t;cin>>t;while(t--){int n,m=0;cin>>n;for(int i=0;i<n;i++){int start,end;cin>>start>>end; work[m].st=start;work[m++].flag=1;work[m].st=end;work[m++].flag=-1;}sort(work,work+m,cmp);//注意要排序 int a[100005]={0},b[100005]={0},ans=0,result=0,sum=0;for(int i=0;i<m;i++){sum+=work[i].flag;if(sum>ans){ans=sum;a[ans]=work[i].st;}}sum=0,ans=0;;for(int i=m-1;i>=0;i--){sum-=work[i].flag;if(sum>ans){ans=sum;b[ans]=work[i].st;}}for(int i=1;i<=ans;i++)result+=b[i]-a[i];cout<<ans<<" "<<result<<endl;}
}

Gene Assembly
可以看作作业调度问题
样例输入
6
340 500
220 470
100 300
880 943
525 556
612 776
3
705 773
124 337
453 665
0
样例输出
3 1 5 6 4
2 3 1

#include <bits/stdc++.h>
using namespace std;
struct node
{int st,ed,flag;
}gene[1005];
bool cmp(node x,node y)
{if(x.ed==y.ed) return(x.st<y.st);else return x.ed<y.ed;
}
int main()
{int n;while(~scanf("%d",&n)&&n){for(int i=1;i<=n;i++){int start,end;scanf("%d%d",&start,&end); gene[i].st=start; gene[i].ed=end; gene[i].flag=i;}sort(gene+1,gene+n+1,cmp);int re[1010]={0};re[1]=gene[1].flag;int j=gene[1].ed,sum=1;for(int i=2;i<=n;i++){if(gene[i].st>j){j=gene[i].ed;re[++sum]=gene[i].flag;}} for(int i=1;i<=sum;i++)cout<<re[i]<<" ";cout<<endl;}return 0;
}

单词排序
直接sort函数省事

#include <bits/stdc++.h>
using namespace std;
int main()
{int n;scanf("%d",&n);string a[27];for(int i=0;i<n;i++)cin>>a[i];sort(a,a+n);for(int i=0;i<n;i++)cout<<a[i]<<" ";return 0;
}

求数组的最长递减子序列
题目描述
给定一个整数序列,输出它的最长递减(注意不是“不递增”)子序列。
输入
输入包括两行,第一行包括一个正整数N(N<=1000),表示输入的整数序列的长度。第二行包括用空格分隔开的N个整数,整数范围区间为[-30000,30000]。
输出
输出最长递减子序列,数字之间有一个空格。
样例输入
8
9 4 3 2 5 4 3 2
样例输出
9 5 4 3 2
思路:
建立一个数组dp用来记录从每个元素出发的最长递减序列,数组b用来记录每个产生递减序列的元素前一个元素的下标,用数组r来记录最后最长的递减序列的元素。
先遍历数组,把每个数组前面的元素所产生的递减序列长度记录在dp,且记录每个递减序列元素下标在b中,将长为sum的递减序列(也就是求得的最长递减序列)依附b,记录在r中,最后从r中输出

#include <bits/stdc++.h>
using namespace std;
int main()
{int a[1005],b[1005],dp[1005],r[1005];int n;cin>>n;for(int i=0;i<n;i++)cin>>a[i];for(int i=0;i<n;i++){dp[i]=1;b[i]=-1;for(int j=0;j<n;j++){if(a[i]<a[j]&&dp[j]+1>dp[i]){dp[i]=dp[j]+1;b[i]=j;}}} int sum=0,ans=0;for(int i=0;i<n;i++){if(sum<dp[i]){sum=dp[i];ans=i;}}for(int i=sum;i>0;i--){r[i]=a[ans];
//      if(b[ans]==-1)
//      break;ans=b[ans];}for(int i=1;i<=sum;i++)cout<<r[i]<<" ";return 0;
}

矩形滑雪场
题目描述
zcb喜欢滑雪。他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形。为了得到更快的速度,滑行的路线必须向下倾斜。 例如样例中的那个矩形,可以从某个点滑向上下左右四个相邻的点之一。例如24-17-16-1,其实25-24-23…3-2-1更长,事实上这是最长的一条。
输入
第1行:两个数字r,c(1 ≤ r, c ≤ 100),表示矩阵的行列。第2…r+1行:每行c个数,表示这个矩阵。
输出
仅一行:输出1个整数,表示可以滑行的最大长度。
样例输入
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
样例输出
25

#include <bits/stdc++.h>
using namespace std;
int a[105][105],len[105][105]={0},dir[4][2]={1,0,-1,0,0,1,0,-1};
int m,n;
int dfs(int x,int y)
{if(len[x][y])return len[x][y];len[x][y]=1;//一定要赋初值 for(int i=0;i<4;i++){int xx=x+dir[i][0];int yy=y+dir[i][1];if(xx>=0&&yy>=0&&xx<m&&yy<n&&(a[xx][yy]<a[x][y]))len[x][y]=max(len[x][y],dfs(xx,yy)+1);}return len[x][y];} int main(){int maxx=0;scanf("%d%d",&m,&n);for(int i=0;i<m;i++){for(int j=0;j<n;j++){scanf("%d",&a[i][j]);  } }for(int i=0;i<m;i++){for(int j=0;j<n;j++){maxx=max(maxx,dfs(i,j));  } }printf("%d",maxx);return 0;}

History Grading
题目描述
Background
Many problems in Computer Science involve maximizing some measure according to constraints.

Consider a history exam in which students are asked to put several historical events into chronological order. Students who order all the events correctly will receive full credit, but how should partial credit be awarded to students who incorrectly rank one or more of the historical events?

Some possibilities for partial credit include:
1 point for each event whose rank matches its correct rank
1 point for each event in the longest (not necessarily contiguous) sequence of events which are in the correct order relative to each other.
For example, if four events are correctly ordered 1 2 3 4 then the order 1 3 2 4 would receive a score of 2 using the first method (events 1 and 4 are correctly ranked) and a score of 3 using the second method (event sequences 1 2 4 and 1 3 4 are both in the correct order relative to each other).
In this problem you are asked to write a program to score such questions using the second method.
The Problem
Given the correct chronological order of n events 1,2,…n as c1,c2,…cn where 1<=ci<=n denotes the ranking of event i in the correct chronological order and a sequence of student responses r1,r2,…rn where 1<=ri<=n denotes the chronological rank given by the student to event i; determine the length of the longest (not necessarily contiguous) sequence of events in the student responses that are in the correct chronological order relative to each other.

输入
The first line of the input will consist of one integer n indicating the number of events with 2<=n<=20 . The second line will contain n integers, indicating the correct chronological order of n events. The remaining lines will each consist of n integers with each line representing a student’s chronological ordering of the n events. All lines will contain n numbers in the range [1…n] , with each number appearing exactly once per line, and with each number separated from other numbers on the same line by one or more spaces.
输出
For each student ranking of events your program should print the score for that ranking. There should be one line of output for each student ranking.
样例输入
4
4 2 3 1
1 3 2 4
3 2 1 4
2 3 4 1
样例输出
1
2
3

#include <bits/stdc++.h>
using namespace std;
int main()
{int n,a[25],b[25],dp[25][25];scanf("%d",&n);for(int i=1;i<=n;i++)//数组一定从1开始定义,否则计算dp数组越界会有奇怪的值 scanf("%d",&a[i]);while(!cin.eof()){memset(dp,0,sizeof(dp));for(int i=1;i<=n;i++)scanf("%d",&b[i]);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){if(a[i]==b[j])dp[i][j]=max(dp[i-1][j-1]+1,dp[i][j]);elsedp[i][j]=max(dp[i-1][j],dp[i][j-1]);}printf("%d\n",dp[n][n]);}return 0;
}

Homework
题目描述
临近开学了,大家都忙着收拾行李准 备返校,但 I_Love_C 却不为此担心! 因为他的心思全在暑假作业上:目前为止还未开动。
暑假作业是很多张试卷,我们这些从试卷里爬出来的人都知道,卷子上的题目有选择题、填空题、简答题、证明题等。而做选择题的好处就在于工作量很少,但又因为选择题题目都普遍很长。如果有 5 张试卷,其中 4 张是选择题,最后一张是填空题,很明显做最后一张所花的时间要比前 4 张长很多。但如果你只做了选择题,虽然工作量很少,但表面上看起来也已经做了4/5的作业了。
I_Love_C决定就用这样的方法来蒙混过关,他统计出了做完每一张试卷所需的时间以及它做完后能得到的价值(按上面的原理,选择题越多价值当然就越高咯)。
现在就请你帮他安排一下,用他仅剩的一点时间来做最有价值的作业。
输入
测试数据包括多组。每组测试数据以两个整数 M,N(1<M<20,1<N<10000) 开头,分别表示试卷的数目和 I_Love_C 剩下的时间。接下来有 M 行,每行包括两个整数 T,V(1<T<N,1<V<10000)分别表示做完这张试卷所需的时间以及做完后能得到的价值,输入以 0 0 结束。
输出
对应每组测试数据输出 I_Love_C 能获得的最大价值。保留小数点 2 位
提示:float 的精度可能不够,你应该使用 double 类型。
样例输入
4 20
4 10
5 22
10 3
1 2
0 0
样例输出
37.00

#include <bits/stdc++.h>
using namespace std;
struct node
{double t,v,arg;//不是double类型会错
}work[25];
bool cmp(node x,node y)
{return x.arg>y.arg;
}
int main()
{int m,n;while(~scanf("%d%d",&m,&n)){if(m==0&&n==0)break;for(int i=0;i<m;i++){scanf("%lf%lf",&work[i].t,&work[i].v);//注意用lf work[i].arg=work[i].v/work[i].t;}sort(work,work+m,cmp);double value=0;int i;for(i=0;i<m;i++){if(n>=work[i].t){value+=work[i].v;n-=work[i].t;}elsebreak;   }if(i<n)value+=1.0*n*work[i].arg;printf("%.2f\n",value);}return 0;
} 

区间包含问题
题目描述
已知 n 个左闭右开区间 [a,b),对其进行 m 次询问,求区间[l,r]最多可以包含 n 个区间中的多少个区间,并且被包含的所有区间都不相交。
输入
输入包含多组测试数据,对于每组测试数据:
第一行包含两个整数 n ,m (1≤n≤100000,1≤m≤100) 。
接下来 n 行每行包含两个整数 a ,b (0≤a<b≤10^9) 。
接下来 m 行每行包含两个整数 l ,r (0≤l<r≤10^9) 。
输出
对于每组测试数据,输出 m 行,每行包含一个整数。
数据过大请使用快速输入输出。
样例输入
4 3
1 3
2 4
1 4
1 2
1 2
1 3
1 4
样例输出
1
1
2

#include <bits/stdc++.h>
using namespace std;
struct node
{int s,e;
}a[100010];
bool cmp(node x,node y)
{return x.e<y.e;
}
int main()
{int n,m;while(~scanf("%d%d",&n,&m)){for(int i=0;i<n;i++)scanf("%d %d",&a[i].s,&a[i].e);sort(a,a+n,cmp);while(m--){int l,r,sum=0;scanf("%d%d",&l,&r);for(int i=0;i<n;i++){if(a[i].e>r) break;if(a[i].s>=l)//注意还有等于 {l=a[i].e;sum++;}}printf("%d\n",sum);}}
}

The Hardest Problem Ever
题目描述
Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked.

You are a sub captain of Caesar’s army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is ‘A’, the cipher text would be ‘F’). Since you are creating plain text out of Caesar’s messages, you will do the opposite:

Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.
输入
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. All characters will be uppercase.

A single data set has 3 components:

Start line - A single line, “START”
Cipher message - A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar
End line - A single line, “END”

Following the final data set will be a single line, “ENDOFINPUT”.
输出
For each data set, there will be exactly one line of output. This is the original message by Caesar.
样例输入
START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT
样例输出
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

#include <bits/stdc++.h>
using namespace std;
int main()
{char str[100]={0};while(gets(str)){if(strcmp(str,"START")==0) continue;if(strcmp(str,"END")==0) continue;if(strcmp(str,"ENDOFINPUT")==0) break;elsefor(int i=0;i<strlen(str);i++){if(str[i]>='A'&&str[i]<='Z')str[i]='A'+(str[i]-'A'+21)%26;}puts(str);//注意这个用的是gets puts来处理字符串 }return 0;
}

最长子序列
题目描述
在一个数组中找出和最大的连续几个数。(至少包含一个数)
例如:
数组A[] = [-2,1,-3,4,-1,2,1,-5,4],则连续的子序列[4,-1,2,1]有最大的和6.
输入
第一行输入一个不超过1000的整数n。
第二行输入n个整数A[i]。
输出
输出一个整数,表示最大的和。
样例输入
3
1 1 -2
样例输出
2

#include <bits/stdc++.h>
using namespace std;
int main()
{int n,a[1005]={0},sum[1005]={0};scanf("%d",&n);for(int i=0;i<n;i++)scanf("%d",&a[i]);sum[0]=a[0];for(int i=0;i<n;i++){if(sum[i]>0)sum[i+1]=sum[i]+a[i+1];elsesum[i+1]=a[i+1];}sort(sum,sum+n);printf("%d",sum[n-1]);
}

元素整除问题
题目描述
输入20个整数,输出其中能被数组中其它元素整除的那些数组元素。

输入
输入20个整数

输出
按输入顺序输出符合要求的数字,每行输出一个整数。

样例输入
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
样例输出
4
6
8
9
10
12
14
15
16
18
20
21

#include <bits/stdc++.h>
using namespace std;
int a[20];
int main()
{for(int i=0;i<20;i++)scanf("%d",&a[i]); for(int i=0;i<20;i++){for(int j=0;j<(a[i]/2);j++)if(a[i]%a[j]==0&&i!=j){printf("%d\n",a[i]);break;}}return 0;
}

渊子赛马
输入
输入有多组测试数据。 每组测试数据包括 3 行: 第一行输入 N。表示马的数量。 第二行有 N 个整型数字,即渊子的 N 匹马的速度。 第三行有 N 个整型数字,即对手的 N 匹马的速度。 当 N 为 0 时退出。

输出
若通过聪明的你精心安排,如果渊子能赢得比赛,那么输出YES。 否则输出NO。

样例输入
5
2 3 3 4 5
1 2 3 4 5
4
2 2 1 2
2 2 3 1
0
样例输出
YES
NO

#include <bits/stdc++.h>
using namespace std;
int a[1010]={0},b[1010]={0};
int main()
{int n;while(~scanf("%d",&n)&&n){int win=0,j=0;for(int i=0;i<n;i++)scanf("%d",&a[i]);for(int i=0;i<n;i++)scanf("%d",&b[i]);sort(a,a+n);sort(b,b+n);for(int i=0;i<n;i++){if(a[i]>b[j]){win++;j++;}}if(win>n/2)printf("YES\n");elseprintf("NO\n");}return 0;
}

The Hardest Problem Ever
Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
样例输入
START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT
样例输出
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

#include <bits/stdc++.h>
using namespace std;
int main()
{char str[100]={0};while(gets(str)){if(strcmp(str,"START")==0) continue;if(strcmp(str,"END")==0) continue;if(strcmp(str,"ENDOFINPUT")==0) break;elsefor(int i=0;i<strlen(str);i++){if(str[i]>='A'&&str[i]<='Z')str[i]='A'+(str[i]-'A'+21)%26;}puts(str);//注意这个用的是gets puts来处理字符串 }return 0;
}

Rock-Paper-Scissors Tournament
题目描述
Rock-Paper-Scissors is game for two players, A and B, who each choose, independently of the other, one of rock, paper, or scissors. A player chosing paper wins over a player chosing rock; a player chosing scissors wins over a player chosing paper; a player chosing rock wins over a player chosing scissors. A player chosing the same thing as the other player neither wins nor loses.
A tournament has been organized in which each of n players plays k rock-scissors-paper games with each of the other players - kn(n-1)/2 games in total. Your job is to compute the win average for each player, defined as w / (w + l) where w is the number of games won, and l is the number of games lost, by the player.
输入
Input consists of several test cases. The first line of input for each case contains 1 <= n <= 100 1 <= k <= 100 as defined above. For each game, a line follows containing p1, m1, p2, m2. 1 <= p1 <= n and 1 <= p2 <= n are distinct integers identifying two players; m1 and m2 are their respective moves (“rock”, “scissors”, or “paper”). A line containing 0 follows the last test case.
输出
Output one line each for player 1, player 2, and so on, through player n, giving the player’s win average rounded to three decimal places. If the win average is undefined, output “-”. Output an empty line between cases.
样例输入
2 4
1 rock 2 paper
1 scissors 2 paper
1 rock 2 rock
2 rock 1 scissors
2 1
1 rock 2 paper
0
样例输出
0.333
0.667

0.000
1.000

#include <bits/stdc++.h>
using namespace std;
int main()
{int m,n;while(~scanf("%d%d",&m,&n)){if(m==0) break;int lose[105]={0},win[105]={0};for(int i=0;i<n*m*(m-1)/2;i++)//可能有多个选手,其号码被记录在p1,p2里 {int p1,p2;char m1[10],m2[10];scanf("%d %s %d %s",&p1,&m1,&p2,&m2);if(m1[0]==m2[0]) continue;if((m1[0]=='p'&&m2[0]=='r')||(m1[0]=='r'&&m2[0]=='s')||(m1[0]=='s'&&m2[0]=='p')){win[p1]++,lose[p2]++;}else{win[p2]++,lose[p1]++;}}for(int i=1;i<=m;i++)if(win[i]+lose[i])//注意存在全赢全输的情况 printf("%.3f\n",1.0*win[i]/(win[i]+lose[i]));elseprintf("-\n");//注意这里 printf("\n"); //记得这里。。。否则格式错误 }   return 0;
}

Balloon Robot
主要思路就是先对机器人在第一个座位的情况的愤怒值之和进行计算得到sum,把解决每道题的tid值进行排序,分别计算机器人使得第i个题愤怒值为0时候的总的愤怒值
1——(i-1)愤怒值增加m-tid[i]
i——p愤怒值减小tid[i]
故result=min(sum+(m-tid[i])*(i-1)-(p-i+1)(tid[i]),result)

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;//必须定义在外面
long long seat[N],tid[N];//注意这里
int main()
{int t;scanf("%d",&t);while(t--){long long n,m,p;scanf("%lld%lld%lld",&n,&m,&p);for(int i=1;i<=n;i++)scanf("%lld",&seat[i]);long long result=2e18,sum=0;for(int i=1;i<=p;i++){long long a,b;scanf("%lld%lld",&a,&b);tid[i]=(seat[a]-b-1+m)%m;sum+=tid[i];}sort(tid+1,tid+p+1);for(int i=1;i<=p;i++){result=min(result,sum+(i-1)*m-p*tid[i]);}printf("%lld\n",result);}
}

sort

#include <bits/stdc++.h>
using namespace std;
int a[1000000];//数组开得太大放里面不行
int main()
{int n,m;while(~scanf("%d%d",&n,&m)){for(int i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n);for(int i=n-1;i>=n-m;i--){if(i==n-m)printf("%d\n",a[i]);//cout<<a[i]<<endl;elseprintf("%d ",a[i]);//cout<<a[i]<<" ";}}return 0;
}


进制转换
题目描述
输入一个十进制正整数,然后输出它所对应的八进制数。
输入
输入一个十进制正整数n(1≤n≤106) 。
输出
输出n对应的八进制数,输出在一行。

#include <iostream>
using namespace std;
int main()
{int m=0,sum=0,x=1;cin>>m;while(m){sum=sum+m%8*x;x=x*10;m=m/8;}cout<<sum;
}

排列问题
题目描述
输入一个可能含有重复字符的字符串,打印出该字符串中所有字符的全排列。
输入
单组测试数据,输入数据是一个长度不超过10个字符的字符串,以逗号结尾。
输出
打印出该字符串中所有字符的全排列。以字典序顺序输出,用空格分隔

#include <bits/stdc++.h>
using namespace std;
int main()
{char str[11];cin.getline(str, 11, ',');int k=0;for(k=0;str[k]!='\0';k++);//注意; sort(str,str+k);do{for(int i=0;i<k;i++){cout<< str[i];}cout<< " ";}while(next_permutation(str,str+k));return 0;
} 

求第k小
题目描述
给定n(1<=n<=1000000)个元素,求第k小数(1<=k<=n)。
输入
一组样例。第一行输入两个整数n和k。第二行输入n个不同的int范围内的数。
输出
输出一行,输出第k小数。

#include <bits/stdc++.h>
using namespace std;
int a[1000001];
int main()
{int m,n;scanf("%d%d",&m,&n);for(int i=0;i<m;i++)scanf("%d",&a[i]);sort(a,a+m);printf("%d",a[n-1]);
}

快速幂
题目描述
输入
多组测试样例,最多50组。每组测试样例给定一个整数x(1<=x<=25000)
输出
对每个样例,输出一行,代表f(x)对100000007取余的结果。

#include <bits/stdc++.h>
using namespace std;
long long poww(int x)
{long long y,b=x,sum=1;//定义一定要用long longwhile(x){if(x&1)sum=sum*b%100000007;x>>=1;b=b*b%100000007;}return sum;
}
int main()
{long long n,result;while(cin>>n){result=1;for(int i=1;i<=n;i++)result=(result+poww(i))%100000007;cout<<result<<endl;}return 0;
}

沙子的质量
题目描述
设有N堆沙子排成一排,其编号为1,2,3,…,N(N< =300)。每堆沙子有一定的数量,可以用一个整数来描述,现在要将N堆沙子合并成为一堆,每次只能合并相邻的两堆,合并的代价为这两堆沙子的数量之和,合并后与这两堆沙子相邻的沙子将和新堆相邻,合并时由于选择的顺序不同,合并的总代价也不相同,如有4堆沙子分别为1 3 5 2我们可以先合并1、2堆,代价为4,得到4 5 2又合并1,2堆,代价为9,得到9 2,再合并得到11,总代价为4+9+11=24,如果第二步是先合并2,3堆,则代价为7,得到4 7,最后一次合并代价为11,总代价为4+7+11=22;问题是:找出一种合理的方法,使总的代价最小。输出最小代价。
输入
第一行一个数N表示沙子的堆数N。 第二行N个数,表示每堆沙子的质量。 a[i]< =1000。
输出
合并的最小代价。
样例输入
4
1 3 5 2
样例输出
22

#include <bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;//注意这里
int dp[305][305],a[305]={0},sum[305]={0};
int main()
{int n;cin>>n;memset(dp,inf,sizeof(dp));for(int i=1;i<=n;i++){cin>>a[i];dp[i][i]=0;sum[i]=sum[i-1]+a[i];}for(int len=2;len<=n;len++){for(int l=1;l<=n-len+1;l++){int r=len+l-1;for(int k=l;k<r;k++)dp[l][r]=min(dp[l][k]+dp[k+1][r]+sum[r]-sum[l-1],dp[l][r]);//注意是sum[l-1] }}cout<<dp[1][n];return 0;
}

最长公共子序列
题目描述
一个字符串A的子串被定义成从A中顺次选出若干个字符构成的串。如A=“cdaad" ,顺次选1,3,5个字符就构成子串" cad" ,现给定两个字符串,求它们的最长共公子串。
输入
第一行两个字符串用空格分开。两个串的长度均小于2000 。
输出
最长子串的长度。
样例输入
abccd aecd
样例输出
3

#include <bits/stdc++.h>
using namespace std;
int dp[2005][2005]={0};
int main()
{char a[2005],b[2005];cin>>a>>b;int len1=strlen(a);int len2=strlen(b);for(int i=1;i<=len1;i++){for(int j=1;j<=len2;j++){if(a[i-1]==b[j-1])dp[i][j]=max(dp[i-1][j-1]+1,dp[i][j]);elsedp[i][j]=max(dp[i-1][j],dp[i][j-1]);}}cout<<dp[len1][len2];return 0;
}

Joseph
杀人游戏,2k个人,报数,报到谁杀谁,最后一个报到数的活下来

#include <bits/stdc++.h>
using namespace std;
int a[16]={0};
int find(int k)
{if(a[k])return a[k];else{for(int i=k+1;;i++){int sum=k*2,flag=0;//每换一次i要回到起点 for(int j=i;flag==0;j+=i-1){if(j>sum)j=j%sum?j%sum:sum;//如果j%sum取余为0,说明杀了最后一个人 if(j<=k)break;elsesum--;if(sum==k) {flag=1;a[k]=i;return a[k];   }}}}
}
int main()
{int n;while(~scanf("%d",&n)&&n)printf("%d\n",find(n));return 0;
}

Factstone Benchmark
题目描述
Amtel has announced that it will release a 128-bit computer chip by 2010, a 256-bit computer by 2020, and so on, continuing its strategy of doubling the word-size every ten years. (Amtel released a 64-bit computer in 2000, a 32-bit computer in 1990, a 16-bit computer in 1980, an 8-bit computer in 1970, and a 4-bit computer, its first, in 1960.)

Amtel will use a new benchmark - the Factstone - to advertise the vastly improved capacity of its new chips. The Factstone rating is defined to be the largest integer n such that n! can be represented as an unsigned integer in a computer word.

Given a year 1960 ≤ y ≤ 2160, what will be the Factstone rating of Amtel’s most recently released chip?
输入
There are several test cases. For each test case, there is one line of input containing y. A line containing 0 follows the last test case.
输出
For each test case, output a line giving the Factstone rating.
样例输入
1960
1981
0
样例输出
3
8

#include <bits/stdc++.h>
using namespace std;
int main()
{int n;while(~scanf("%d",&n)&&n){double k=log(4);//注意double类型for(int i=1960;i<=n;i=i+10)k*=2;double f=0;//注意double类型int j;for(j=1;f<k;j++)f+=log(j);printf("%d\n",j-2);}return 0;
}

Ants
题目描述
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
输入
The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.
输出
For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
样例输入
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191
样例输出
4 8
38 207

#include <bits/stdc++.h>
using namespace std;
int maxx[100001],minn[100001];
int main()
{int p;cin>>p;while(p--){int l,n;cin>>l>>n;int distance;for(int i=0;i<n;i++){cin>>distance;minn[i]=min(distance,l-distance);maxx[i]=max(distance,l-distance);}sort(minn,minn+n);sort(maxx,maxx+n);cout<<minn[n-1]<<" "<<maxx[n-1]<<endl;}return 0;
}

sort2
题目描述
给你n个整数,请按从大到小的顺序输出其中前m大的数。
输入
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个都处于区间[-500000,500000]的整数,整数可能会重复出现。
输出
对每组测试数据按从大到小的顺序输出前m大的数。

#include <bits/stdc++.h>
using namespace std;
int a[1000000];
int main()
{int m,n;while(~scanf("%d%d",&m,&n)){for(int i=0;i<m;i++)scanf("%d",&a[i]);sort(a,a+m);for(int i=m-1;i>m-n;i--)printf("%d ",a[i]);printf("%d\n",a[m-n]);}return 0;}

2021-10-30相关推荐

  1. 2021/10/30的1+X大数据Java答案

    2021/10/30 步骤二 public Member() { }public Member(String name,String pwd,float score,int rank) {this.n ...

  2. [2021.10.30][uml]UML顺序图规范

    1 生命线 一般为虚线 2 消息 (1)创始消息 实心圆开头,实心箭头 (2)同步消息 实心箭头 3 控制期 表示阻塞调用 4 返回值 5 自身消息 6 创建实例 虚线实心箭头,新创建的对象实例画在创 ...

  3. 2021卓见杯第三届CCPC河南省省赛所有题超详细题解附加榜单真题解析,简单代码+详细注释+思想,要看的,补题的速速点进来 2021 10.30

    本人现在比较菜,所以难免出现错误,文章中有不太恰当地方,还请大家指正. 是否因为出题人的简短题解而发愁?,是否看不懂出题人的变态模板标程?是否因为自己是小白而苦恼?来看这片文章,帮助你解决这些问题 题 ...

  4. 第31课 3721数-2021.10.30 《小学生C++趣味编程》

    /* 试编一程序,输出200以内所有的"3721"数. 除以3余2 同时除以7余1的数叫"3721" */ #include<iostream> # ...

  5. Day19-22 2021/10/13-16 JAVA贪吃蛇 全注释版

    Day19-22 2021/10/13-16 贪吃蛇 帧:如果时间足够小 就是动画 键盘监听 定时器 Timer package snale; import javax.swing.*; /*** @ ...

  6. 2021.10.25-10.31 AI行业周刊(第69期):AI进化之路

    本周<Opencv基础及AI项目实战>以及<Pytorch模型推理及多任务通用范式>两门课程已经完结. 两门课程中,针对所有完成作业的同学,都颁发了毕业证书. 并且对于完成比较 ...

  7. Python 最近两条好消息:①TIOBE排名超过C和Java②新版本发布3.10.0,还有今天刚发布的《What’s New in Python(2021.10.15)》

    来自TIOBE的最新10月份统计数据显示,Python首次超越Java.JavaScript.C语言等,成为最受欢迎的编程语言.TIOBE过去20年一直在追踪编程语言的受欢迎程度,其数据来自于对25个 ...

  8. leetcode刷题记录2:进度64, 2021.10.23

    文章目录 数组的度(题目编号697:[link](https://leetcode-cn.com/problems/degree-of-an-array/)) 二叉搜索树(题目编号700:[link] ...

  9. 2021.10.9-2021.10.15-本周代码+如何激励自己

    目录 本周学术总结: 本周生活总结: 本周代码 本周学术总结: 本周完成了MOOC的翁恺老师C语言课程的1-7周,目前第七周的数组还有点问题,本周末可以把第八周干完,然后复习一下.这两天还要再规划一下 ...

  10. JZOJ 7036. 2021.03.30【2021省赛模拟】凌乱平衡树(平衡树单旋+权值线段树)

    JZOJ 7036. 2021.03.30[2021省赛模拟]凌乱平衡树 题目大意 给出两棵Treap,大小分别为 n , m n,m n,m,每个点的 p r i o r i t y priorit ...

最新文章

  1. 上拉加载你这个坑货~
  2. A good debug parameter - sap-ds-debug=true
  3. leetcode 存在重复元素
  4. pdf如何解除加密?
  5. arcmap中有火星坐标码_GIS转换之火星坐标系转换
  6. 解决国外链接下载软件速度慢的方法
  7. Office的许可证不是正版弹框解决
  8. STM32解析航模遥控器的PPM信号
  9. mes系统是什么?mes系统的主要功能有哪些?
  10. 微软的人立方关系图竟然用的Flash!!!
  11. 各种数据库的应用场景
  12. 使用Python获取股票解禁数据并绘制股价曲线
  13. cocos2dx抖动动作
  14. 基于随机森林的假新闻检测项目
  15. java虚拟机收集器_Java虚拟机(JVM)垃圾回收器G1收集器 - Break易站
  16. 以一种另类的姿态,怀念海子
  17. 电赛专题---一.概述【电赛简介 /信号类需要准备什么?/怎么才能打好电赛?】
  18. KDZD8600漏电保护检测仪
  19. 关于“软件工程是不是教会不怎么写程序的人开发软件?”的看法。
  20. 织梦响应式日化食品零食连锁加盟店企业网站模板

热门文章

  1. Open CASCADE安装及+MFC
  2. Docker学习(三):复杂软件安装-主从mysql及redis集群
  3. 栈详解(顺序栈和链栈)
  4. 获取textarea标签中的换行符和空格
  5. 从苏宁电器到卡巴斯基(第二部)第30篇:我当高校教师的这几年 VI
  6. textarea中的内容保存与显示时换行符的处理方法
  7. Core Dump技术介绍
  8. Docker学习——DockerFile
  9. 离散数学-10 群与环
  10. CodeSys轴控指令使用方法