Codeforces Round #260 (Div. 2) 题解

A. Laptops

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop.

Please, check the guess of Alex. You are given descriptions of n laptops. Determine whether two described above laptops exist.

Input

The first line contains an integer n (1 ≤ n ≤ 105) — the number of laptops.

Next n lines contain two integers each,ai andbi(1 ≤ ai, bi ≤ n), whereai is the price of thei-th laptop, and bi is the number that represents the quality of thei-th laptop (the larger the number is, the higher is the quality).

All ai are distinct. Allbi are distinct.

Output

If Alex is correct, print "Happy Alex", otherwise print "Poor Alex" (without the quotes).

Sample test(s)
Input
2
1 2
2 1
Output
Happy Alex
解题思路:

排序,扫一遍即可。

传送门:点击打开链接

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int MAXN = 1e5 + 10;
typedef pair<int ,int> P;
P p[MAXN];
int n;

bool cmp(const P &a, const P &b)
{
    if(a.first == b.first) return a.second < b.second;
    return a.first < b.first;
}

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; ++i)
        scanf("%d%d", &p[i].first, &p[i].second);
    sort(p, p+n, cmp);
    bool flag = false;
    for(int i = 0; i < n-1; i++)
    {
        if(p[i].first<p[i+1].first && p[i].second>p[i+1].second)
        {
            flag = true;
            break;
        }
    }
    printf("%s\n", flag ? "Happy Alex" : "Poor Alex");
    return 0;
}

B. Fedya and Maths

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:

(1n + 2n + 3n + 4nmod 5

for given value of n. Fedya managed to complete the task. Can you? Note that given numbern can be extremely large (e.g. it can exceed any integer type of your programming language).

Input

The single line contains a single integer n (0 ≤ n ≤ 10105). The number doesn't contain any leading zeroes.

Output

Print the value of the expression without leading zeros.

Sample test(s)
Input
4
Output
4
Input
124356983594583453458888889
Output
0

传送门:点击打开链接

解题思路:

找规律,周期为4,前四个值为0,0,0,4,当n是4的整数倍时,结果为4.判断一个数是否为4的整数倍,只需要看最后两位是否为4的整数倍。

代码:

#include <cstdio>
#include <cstring>

const int MAXN = 1e5 + 10;
char s[MAXN];

int main()
{
    scanf("%s", s);
    int len = strlen(s);
    int ans = 0, ret = 0;
    if(1 == len)
    {
        ret = s[0] - '0';
        if(0 == ret%4) ans = 4;
    }
    else
    {
        ret = (s[len-2]-'0')*10 + s[len-1]-'0';
        if(0 == ret%4) ans = 4;
    }
    printf("%d\n", ans);
    return 0;
}


C. Boredom

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.

Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence. That step brings ak points to the player.

Alex is a perfectionist, so he decided to get as many points as possible. Help him.

Input

The first line contains integer n (1 ≤ n ≤ 105) that shows how many numbers are in Alex's sequence.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105).

Output

Print a single integer — the maximum number of points that Alex can earn.

Sample test(s)
Input
2
1 2
Output
2
Input
3
1 2 3
Output
4
Input
9
1 2 1 3 2 2 2 2 3
Output
10
传送门: 点击打开链接

解题思路:

dp,先对输入的数据hash一下,用a[i]表示i的个数,用dp[i+1]表示最后一个被选中的数是i+1时,最大的点数,我们可以得到状态转移方程:dp[i+1] = max(dp[i-1]+(i+1)*a[i+1],dp[i-2]+(i+1)*a[i+1]),我们找出序列中出现的最大值mx,dp[i](i=1,2,3...,mx+2)中的最大值即为答案。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long lint;
const int MAXN = 1e5 + 10;
int n, a[MAXN];
lint dp[MAXN];

int main()
{
    scanf("%d", &n);
    int mx = -1;
    memset(dp, 0, sizeof(dp));
    for(int i = 0; i < n; ++i)
    {
        int x;
        scanf("%d", &x);
        if(x > mx) mx = x;
        a[x]++;
    }
    dp[1] = a[1], dp[2] = a[2]*2;
    lint ans = 0;
    for(int i = 2; i <= mx+1; i++)
    {
        dp[i+1] = max(dp[i-1]+1ll*(i+1)*a[i+1], dp[i-2]+1ll*(i+1)*a[i+1]);
        if(dp[i+1] > ans) ans = dp[i+1];
    }
    printf("%I64d\n", ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值