2014ACM/ICPC亚洲区北京站

http://acm.hdu.edu.cn/showproblem.php?pid=5112

输入n个时刻和位置,问那两个时刻间速度最快。

解法:按照时间排序,然后依次求相邻两个之间的速度,速度=ds/dt

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int M=1e4+10;
 5 struct G{
 6     int t,x;
 7     friend bool operator <(const G &a,const G &b){
 8         return a.t<b.t;
 9     }
10 }g[M];
11 int main(){
12     int t,n;
13     while(~scanf("%d",&t)){
14         int cas=1;
15         while(t--){
16             scanf("%d",&n);
17             for(int i=0;i<n;i++){
18                 scanf("%d%d",&g[i].t,&g[i].x);
19             }
20             sort(g,g+n);
21             double ans=0;
22             for(int i=0;i<n-1;i++){
23                 ans=max(ans,abs(g[i].x-g[i+1].x)*1.0/(g[i+1].t-g[i].t));
24             }
25             printf("Case #%d: %.2f\n",cas++,ans);
26         }
27     }
28     return 0;
29 }

http://acm.hdu.edu.cn/showproblem.php?pid=5122

输入n的一个排列,问最少多少次操作能将其从小到大排好序, 每次操作可以任意选一个数,不断的往后交换直至后一个大于他停止。

解法:首先选必须要从大往小选,这样最多n次就能排好序,就是逆序的情况。因为如果选个中间的,他往后换的过程可能会遇到一个较大的使得他停下,没达到需要到的位置,从大到小选就避免了这种情况,是最优的方法。问题转化为如何统计步数。我们定义一个需求need,表示当前需要选的数,定义一个指针p,表示当前位置,指针从最后一个位置开始,需求从n开始。每次迭代,若指针的位置的值a【p】恰好等于need,则我们不需要浪费步数,数就在他应该在的位置。若a【p】小于need,说明这个位置应该放的是need,我们需要消耗一步,将前面的need移动至此,但我们不需改动数组的值,因为改动的复杂度是on的。此时我们只需要记录ans++,need--,p不动。若a【p】大于need,说明这个值已经处理过了,p--。

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int M=1e6+10;
 5 int a[M];
 6 int main(){
 7     int t,n;
 8     while(~scanf("%d",&t)){
 9         int cas=1;
10         while(t--){
11             scanf("%d",&n);
12             for(int i=0;i<n;i++){
13                 scanf("%d",&a[i]);
14             }
15             int ans=0;
16             int p=n-1;
17             for(int i=n;i>=1;i--){
18                 if(a[p]==i){
19                     p--;
20                     continue;
21                 }
22                 if(a[p]>i){
23                     p--;
24                     i++;
25                     continue;
26                 }
27                 ans++;
28             }
29             printf("Case #%d: %d\n",cas++,ans);
30         }
31     }
32     return 0;
33 }

end

时间: 2024-10-09 06:20:33

2014ACM/ICPC亚洲区北京站的相关文章

K.Bro Sorting(杭电5122)(2014ACM/ICPC亚洲区北京站)

K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 67    Accepted Submission(s): 39 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubbl

2014ACM/ICPC亚洲区北京站-重现赛 [B.Black And White] 涂色DFS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 题目大意:在一个N * M的棋盘里涂色,要求i颜色要涂ci次.ci求和为N * M. 关键思想:从第一个点开始着色,一行一行涂,注意一旦找到答案后面就不必搜,当剩下个数为n时若有种颜色>n/2上取整就不必搜了(!). #include <iostream> #include <iomanip> #include <cstdio> #include <cst

2014ACM/ICPC亚洲区北京站题解

本题解不包括个人觉得太水的题(J题本人偷懒没做). 个人觉得这场其实HDU-5116要比HDU-5118难,不过赛场情况似乎不是这样.怀疑是因为老司机带错了路. 这套题,个人感觉动态规划和数论是两个主要的考点. HDU 5113 Black And White HDU 5114 Collision HDU 5116 Everlasting L HDU 5117 Fluorescent HDU 5118 GRE Words Once More!

2014ACM/ICPC亚洲区北京站-A-(Curious Matt)

A Curious Matt                                   Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Problem Description There is a curious man called Matt. One day, Matt's best friend Ted is wandering on the non-neg

A Curious Matt(2014ACM/ICPC亚洲区北京站)

A Curious Matt Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description There is a curious man called Matt. One day, Matt's best friend Ted is wander

Bro Sorting(2014ACM/ICPC亚洲区北京站-K)

Bro Sorting Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble so

A Curious Matt(2014ACM/ICPC亚洲区北京站-A)

A Curious Matt Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description There is a curious man called Matt. One day, Matt's best friend Ted is wander

2014ACM/ICPC亚洲区北京站-A Curious Matt

A Curious Matt Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 0 Accepted Submission(s): 0 Problem Description There is a curious man called Matt. One day, Matt's best friend Ted is wandering on

HDU 5112 A Curious Matt (2014ACM/ICPC亚洲区北京站-重现赛)

A Curious Matt Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 3058    Accepted Submission(s): 1716 Problem Description There is a curious man called Matt. One day, Matt's best friend Ted is w