AtCoder Beginner Contest 160 做题感想

5 篇文章 0 订阅
3 篇文章 0 订阅

AtCoder Beginner Contest 160 做题感想

这是我的博客,有兴趣的可以参观一下。

A - Coffee

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.
Given a string S, determine whether it is coffee-like.

Constraints

S is a string of length 6 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

Sample Input 2

iphone

Sample Output 2

No

一点小想法

签到题,只需要先判断第 3 个字符和第 4 个字符是否相同,第 5 个和第 6 个字符是否相同。如果都相同就输出 Yes 否则输出 No

代码

#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
string s;

int main() {
    cin >> s;
    if(s[2] == s[3] && s[4] == s[5])
        puts("Yes");
    else
        puts("No");
    return 0;
}

B - Golden Coins

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 200 points

Problem Statement

Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.)Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn?(We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.)

Constraints

  • 0 ≤ X ≤ 1 0 9 0≤X≤10^9 0X109
  • X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the maximum number of happiness points that can be earned.

Sample Input 1

1024

Sample Output 1

2020

一点小想法

这是一道简单的贪心题,我们需要尽可能的用 500 去兑换快乐值,然后余下的用 5 去兑换。

代码

#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
ll x;

int main() {
    scanf("%lld", &x);
    ll ans = 0;
    ans += 1000 * (x / 500);
    x %= 500;
    ans += 5 * (x / 5);
    printf("%lld\n", ans);
    return 0;
}

C - Traveling Salesman around Lake

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.The i-th house is built at a distance of Ai meters from the northmost point of the pond, measured clockwise around the pond.When traveling between these houses, you can only go around the pond.Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

  • 2 ≤ K ≤ 1 0 6 2≤K≤10^6 2K106
  • 2 ≤ N ≤ 2 × 1 0 5 2≤N≤2×10^5 2N2×105
  • 0 ≤ A 1 < . . . < A N < K 0≤A_1<...<A_N<K 0A1<...<AN<K
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

K, N

A 1 A 2 . . . A N A_1 A_2 ... A_N A1A2...AN

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

一点小想法

对于每一个房子,有两种方法可以走遍整个池塘,一种是顺时针走,一种是逆时针走,我们只需要把这两种方法的代价算出来,然后选择代价较小的即可。

代码

#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <iostream>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int Max = 1e6 + 5;
int k, n, a[Max];

int main() {
    scanf("%d %d", &k, &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    int ans = inf;
    for (int i = 1; i <= n; i++) {
        int p1 = inf, p2 = inf;
        if(i == 1) {
            p1 = a[n] - a[1];
            p2 = a[1] + (k - a[2]);
        }
        else if(i == n) {
            p1 = (k - a[n]) + a[n - 1];
            p2 = a[n] - a[1];
        }
        else {
            p1 = (k - a[i]) + a[i - 1];
            p2 = a[i] + (k - a[i + 1]);
        }
        ans = min(ans, min(p1, p2));
    }
    printf("%d\n", ans);
    return 0;
}

D - Line++

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

  • For each i=1,2,…,N−1, there is an edge between Vertex ii and Vertex i+1.
  • There is an edge between Vertex X and Vertex Y.

For each k=1,2,…,N−1, solve the problem below:

  • Find the number of pairs of integers ( i , j i,j i,j)( 1 ≤ i < j ≤ N 1≤i<j≤N 1i<jN) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

  • 3 ≤ N ≤ 2 × 1 0 3 3≤N≤2×10^3 3N2×103
  • 1 ≤ X , Y ≤ N 1≤X,Y≤N 1X,YN
  • X + 1 < Y X+1<Y X+1<Y
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k = 1 , 2 , . . . , N − 1 k=1,2,...,N−1 k=1,2,...,N1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

一点小想法

我们暂且把那条直接连通 x , y 的边称为桥,那么对于每两个点有两条路可走一种是走桥,一种是不走桥。同理上一道题,我们只需要把两种走法的距离都算出来,然后取最小的即可。

在记录算的过程中可以用一个 ans 数组把每种答案的数量都记录下来用于输出。

代码

#include <cstdio>
#include <algorithm>
using namespace std;
const int Max = 1e4 + 5;
int n, x, y, ans[Max];

int main() {
    scanf("%d %d %d", &n, &x, &y);
    for (int i = 1; i <= n - 1; i++) {
        for (int j = i + 1; j <= n; j++) {
            int dis = j - i;
            dis = min(dis, abs(i - x) + abs(j - y) + 1);
            dis = min(dis, abs(j - x) + abs(i - y) + 1);
            ans[dis]++;
        }
    }
    for (int i = 1; i <= n - 1; i++)
        printf("%d\n", ans[i]);
    return 0;
}

E - Red and Green Apples

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.
You have A red apples of deliciousness p 1 , p 2 , … , p A p_1,p_2,…,p_A p1,p2,,pA, B green apples of deliciousness q 1 , q 2 , … , q B q_1,q_2,…,q_B q1,q2,,qB, and C colorless apples of deliciousness r 1 , r 2 , … , r C r_1,r_2,…,r_C r1,r2,,rC.
Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.
From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.
Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

  • 1 ≤ X ≤ A ≤ 1 0 5 1≤X≤A≤10^5 1XA105
  • 1 ≤ Y ≤ B ≤ 1 0 5 1≤Y≤B≤10^5 1YB105
  • 1 ≤ C ≤ 1 0 5 1≤C≤10^5 1C105
  • 1 ≤ p i ≤ 1 0 9 1≤p_i≤10^9 1pi109
  • 1 ≤ q i ≤ 1 0 9 1≤q_i≤10^9 1qi109
  • 1 ≤ r i ≤ 1 0 9 1≤r_i≤10^9 1ri109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p 1 p 2 . . . p A p_1 p_2 ... p_A p1p2...pA
q 1 q 2 . . . q B q_1 q_2 ... q_B q1q2...qB
r 1 r 2 . . . r C r_1 r_2 ... r_C r1r2...rC

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

一点小想法

还是一道贪心题,我们可以先不考虑无色苹果,我们可以先通过排序找出美味值最大的 X 个红苹果,Y 个绿苹果,然后再将无色苹果按照降序排序,这时我们把红苹果和绿苹果看做一种苹果,将 X + Y 个红绿苹果再进行升序排序,最后用无色苹果换掉比它美味值小的有色苹果。

代码

#include <cstdio>
#include <algorithm>
using namespace std;
#define ll long long
const int Max = 1e6 + 5;
ll x, y, A, B, C, a[Max], b[Max], c[Max];
ll ans[Max];
struct cmpFunctor {
    inline bool operator() (const ll& x, const ll& y) {
        return x > y;
    }
};
int main() {
    scanf("%lld %lld %lld %lld %lld", &x, &y, &A, &B, &C);
    for (int i = 1; i <= A; i++)
        scanf("%lld", &a[i]);
    for (int i = 1; i <= B; i++)
        scanf("%lld", &b[i]);
    for (int i = 1; i <= C; i++)
        scanf("%lld", &c[i]);
    sort(a + 1, a + A + 1, cmpFunctor());
    sort(b + 1, b + B + 1, cmpFunctor());
    sort(c + 1, c + C + 1, cmpFunctor());
    int cnt = 0;
    for (int i = 1; i <= x; i++)
        ans[++cnt] = a[i];
    for (int i = 1; i <= y; i++)
        ans[++cnt] = b[i];
    sort(ans + 1, ans + cnt + 1, cmpFunctor());
    int tot = 1;
    for (int i = cnt; i >= 1; i--) {
        if(ans[i] < c[tot]) {
            ans[i] = c[tot];
            tot++;
        }
        if(tot > C)
            break;
    }
    ll Ans = 0;
    for (int i = 1; i <= cnt; i++)
        Ans += ans[i];
    printf("%lld\n", Ans);
        return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值