A. Laptops
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.
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.
If Alex is correct, print "Happy Alex", otherwise print "Poor Alex" (without the quotes).
2 1 2 2 1
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
Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:
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).
The single line contains a single integer n (0 ≤ n ≤ 10105). The number doesn't contain any leading zeroes.
Print the value of the expression without leading zeros.
4
4
124356983594583453458888889
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
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.
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).
Print a single integer — the maximum number of points that Alex can earn.
2 1 2
2
3 1 2 3
4
9 1 2 1 3 2 2 2 2 3
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;
}