Ambiguous Dates Gym-101522A

Ambiguous Dates Gym-101522A

标签:思维&逻辑


题目链接

解法一

/*
    题意:日期有两种表示day/month/yearmonth/day/year7/8/2017(8/7/2017)是引起歧义的, 而15/8/201710/10/2017则不会,
          要求重新排列月份,使得引起歧义的日期最少,
          一年可以有M月(1 <= M <= 10^5),每月有D天(1 <= D <= 10^5)。
    分析:某个月份的天数越多,这个月就应该越靠后。排序后,计算引起歧义的日期数量即可。
*/
#include <stdio.h>
#include <algorithm>
using namespace std;

#define maxn 100005
int month[maxn], M;

int main(){
    while(scanf("%d", &M) != EOF){
        int i, top = 1;
        __int64 num = 0, ans = 0;

        for(i = 1; i <= M; i++)  scanf("%d", &month[i]);
        sort(month + 1, month + M + 1);
        for(i = 1; i <= M; i++)
            if(month[i] > i)  ans += 2 * min(month[i] - i, M - i);
        printf("%lld\n", ans);
    }

    return 0;
}

解法二

#include <stdio.h>
#include <algorithm>
using namespace std;

#define maxn 100005
int month[maxn], M;

int main(){
    while(scanf("%d", &M) != EOF){
        int i, top = 1;
        __int64 num = 0, ans = 0;

        for(i = 1; i <= M; i++)  scanf("%d", &month[i]);
        sort(month + 1, month + M + 1);
        for(i = 1; i <= M; i++){
            while(i != 1 && top < i && month[top] < i){
                top++;
                num--;
            }
            //printf("num: %d\n", num);
            ans += num;
            num++;
        }
        printf("%lld\n", ans * 2);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值