hdu 5174(计数)

Ferries Wheel

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1651    Accepted Submission(s): 494


Problem Description
The Ferries Wheel is a circle,rounded by many cable cars,and the cars are numbered 1,2,3...K1,K in order.Every cable car has a unique value and A[i1]<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 (k1)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
Hint
In 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.
 
 
题意:有n个人,每个人都有一个 val ,如果 val 相同,就证明他们在同一个摩天轮的小格子里面,现在这 n 个人要座到摩天轮上去,每个摩天轮格子都有一个价值,现在如果某个格子的价值是 val ,左边是 val1 ,右边是 val2 ,如果 (val+val1)%MAX_INT == val2 ,那么这个里面的人就会得到Misaki一个吻,问最后有多少人会得到Misaki 的吻。
题解:排序后,统计每个摩天轮格子里面的人的个数,去重后计数。当只有一个格子时输出 -1 .
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = 105;
typedef long long LL;
const LL mod = 2147483647;
LL val[N];
int cnt[N];
int main()
{
    int n;
    int t = 1;
    while(scanf("%d",&n)!=EOF){
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;i++){
            scanf("%lld",&val[i]);
        }
        sort(val+1,val+1+n);
        int k =2;
        int num=1;
        cnt[num]++;
        for(int i=2;i<=n;i++){
            if(val[i]==val[i-1]){
                cnt[num]++;
            }else{
                num++;
                cnt[num]++;
                val[k++] = val[i];
            }
        }
        printf("Case #%d: ",t++);
        k--;
        if(k==1){
            printf("-1
");
            continue;
        }
        int ans = 0;
        for(int i=1;i<=k;i++){
            if(i==1){
                if((val[1]+val[k])%mod==val[2]) ans+=cnt[1];
            }else if(i==k){
                if((val[k-1]+val[k])%mod==val[1]) ans+=cnt[k];
            }else{
                if((val[i]+val[i-1])%mod==val[i+1]) ans+=cnt[i];
            }
        }
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/liyinggang/p/5684935.html