HDU 5147 Ferries Wheel(暴力 数组)——BestCoder Valentine's Day Round
2015-08-25 19:16
447 查看
Ferries Wheel
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Problem Description
The Ferries Wheel is a circle,rounded by many cable cars,and the cars are numbered 1,2,3...K−1,K in
order.Every cable car has a unique value and A[i−1]<A[i]<A[i+1](1<i<K).
Today,Misaki invites N friends
to play in the Ferries Wheel.Every one will enter a cable car. One person will receive a kiss from Misaki,if this person satisfies the following condition: (his/her cable car's value + the left car's value
) % INT_MAX = the right car's value,the 1st car’s
left car is the kth car,and
the right one is 2nd car,the kth car’s
left car is the (k−1)th car,and
the right one is the 1st car.
Please help Misaki to calculate how many kisses she will pay,you can assume that there is no empty cable car when all friends enter their cable cars,and one car has more than one friends is valid.
Input
There are many test cases.
For each case,the first line is a integer N(1<=N<=100) means
Misaki has invited N friends,and
the second line contains N integers val1,val2,...valN,
the val[i] means
the ith friend's
cable car's value.
(0<=val[i]<= INT_MAX).
The INT_MAX is 2147483647.
Output
For each test case, first output Case #X: ,then output the answer, if there are only one cable car, print "-1";
Sample Input
3 1 2 3 5 1 2 3 5 7 6 2 3 1 2 7 5
Sample Output
Case #1: 1 Case #2: 2 Case #3: 3
HintIn the third sample, the order of cable cars is {{1},{2}, {3}, {5}, {7}} after they enter cable car,but the 2nd cable car has 2 friends,so the answer is 3.
Source
Valentine's Day Round
/************************************************************************/
附上该题对应的中文题
Ferries Wheel
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
问题描述
摩天轮是一个环,周围围绕着一些缆车。每个缆车按顺序编号为1,2,3...K-1,K1,2,3...K−1,K而且每个缆车也拥有一个唯一的值且保证A[i-1] < A[i] < A[i+1](1 < i < K)A[i−1]<A[i]<A[i+1](1<i<K);
Misaki 邀请NN个朋友去做摩天轮,每个朋友都进入一个缆车,如果哪个朋友满足:"(他的缆车的值+左边一个缆车的值)%INT_MAX=右边一个缆车的值",那么可以得到Misaki的一个吻,第1个缆车的左边是第KK个车,右边是第2个车,第KK个车的左边是第k-1k−1个,右边是第1个.
请帮Misaki计算一下她要吻多少次。你可以假设当所有人进入缆车后,没有空缆车,一个车装有多个朋友也是合法的.
输入描述
多组测试数据 每组测试数据第一行一个NN表示有NN个朋友。 第二行有NN个整数,val[i]val[i]表示第ii个朋友的缆车的值。 1<=n<=1001<=n<=100; 0<=val[i]<=0<=val[i]<= INT_MAX INT_MAX 为 21474836472147483647
输出描述
对于每组测试数据,输出Case #x: answer; 如果只有一个缆车,输出-1.
输入样例
3 1 2 3 5 1 2 3 5 7 6 2 3 1 2 7 5
输出样例
Case #1: 1 Case #2: 2 Case #3: 3
Hint
对于第三个样例,当他们进入缆车后,缆车的值是{{1},{2}, {3}, {5}, {7}},但第二个缆车有两个朋友,所以答案是3.
/****************************************************/
出题人的解题思路:
题意:有NN个人去做摩天轮,每个人进一个缆车,问有多少人满足:【(他的缆车的值+左边车的值)%INT_MAX=右边缆车的值】.
解法:暴力,记录每个缆车出现的次数,排序去重,枚举缆车的值,判断是否满足那个等式即可。 HACK点:题目没告诉你输入是有重复数的(开始没有样例3,担心被喷得太惨,就加了样例3),只有一个缆车的情况不一定只是N=1N=1.
正如出题人所说,该题纯暴力,之所以一开始需要排序去重,无非是因为题目提到了这么一句话“Every
cable car has a unique value and A[i−1]<A[i]<A[i+1](1<i<K).”,而且在去重过程中还需记录每个价值的缆车出现的次数,原因见Hint。若排序去重之后,此时只有一个缆车的话,那么输出“-1”;否则,接下来则是暴力判断题中所给的等式即可。
有一点稍微提一提,INT_MAX的值是int型的最大值,要对此求余,不用看,缆车本身值+左边车的值必定是会超过int型的,所以中间过程记得转化一下,__int64也好,long long也罢,都是可以的
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<math.h> #include<vector> #include<map> #include<set> #include<stdlib.h> #include<cmath> #include<string> #include<algorithm> #include<iostream> #define exp 1e-10 using namespace std; const int N = 105; const int inf = 2147483647; const int mod = 1000000007; int s ,v ,t ; int main() { int n,i,k,j=1,ans; while(~scanf("%d",&n)) { memset(t,0,sizeof(t)); for(i=0;i<n;i++) scanf("%d",&s[i]); sort(s,s+n); v[0]=s[0];t[0]=1; for(k=0,i=1;i<n;i++) if(s[i]!=s[i-1]) { v[++k]=s[i]; t[k]=1; } else t[k]++; if(!k) printf("Case #%d: -1\n",j++); else { for(ans=i=0;i<=k;i++) if(((__int64)v[i]+v[(i+k)%(k+1)])%INT_MAX==v[(i+1)%(k+1)]) ans+=t[i]; printf("Case #%d: %d\n",j++,ans); } } return 0; }菜鸟成长记
相关文章推荐
- CSS的注释部分编程引申
- 觉得还不错的两个js时间轴插件
- javascript钩子之Backbone里的实现
- jquery声明
- HTML访问CGI
- 制作好看又非常简单CSS样式的颜色块
- JavaScript中使用prototype来扩展对象,举例
- JavaScript学习笔记
- 探索javascript中函数的执行顺序
- jquery或者js对html控件的处理汇总
- jQuery on重复邦定事件
- 基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理
- jquery 直接访问图片路径
- css知多少(6)——选择器的优先级
- package.json字段全解
- JS实现图片延迟加载
- JS根据key值获取URL中的参数值,以及把URL的参数转换成json对象
- JSP+Servlet + JDBC 实现简单的登录验证模块
- 备忘篇——正则表达式集合
- angularJs学习笔记