Codeforces Round #264 (Div. 2)题解

本文详细介绍了Codeforces Round #264 (Div. 2)中的几道题目,包括买糖获得最大糖果数量的问题、在游戏中最小花费到达终点的问题、在棋盘上放置两个主教获取最多奖励的问题,以及寻找多个排列最长公共子序列的长度。通过实例解析了每道题目的解题思路和最优策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A题:
A. Caisa and Sugar
time limit per test
1 second
memory limit per test
256 megabytes

Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town.

Unfortunately, he has just s dollars for sugar. But that's not a reason to be sad, because there are n types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar).

Caisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change.

Input

The first line contains two space-separated integers n, s (1 ≤ n, s ≤ 100).

The i-th of the next n lines contains two integers xi, yi (1 ≤ xi ≤ 100; 0 ≤ yi < 100), where xi represents the number of dollars and yi the number of cents needed in order to buy the i-th type of sugar.

Output

Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar.

Sample test(s)
Input
5 10
3 90
12 0
9 70
5 50
7 0
Output
50
Input
5 5
10 10
20 20
30 30
40 40
50 50
Output
-1
Note

In the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change.


对于A题没有难度,直接其实就是将所有钱数划归成为分,即总钱数等于xi*1·00+yi,然后就是(s*100-总钱数)%100,输出最大的即可AC,没有难度。。。也就不多说了,具体代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int a[105],b[105],sum[105];
int ma(int a,int b)
{
    if(a>b)return a;
    return b;
}
int main()
{
 //   freopen("in.txt","r",stdin);
    int n,s;
    while(cin>>n>>s)
    {
        for(int i=0;i<n;i++)
        {
            cin>>a[i]>>b[i];
            sum[i]=a[i]*100+b[i];
        }
        int maxx=0,flag=0;
        for(int i=0;i<n;i++)
        {
            if(100*s>=sum[i])
            {
                int ti=100*s-sum[i];
                maxx=ma(maxx,ti%100);
                flag=1;
            }
        }
        if(flag==0)
            cout<<-1<<endl;
        else
            cout<<maxx<<endl;
    }
    return 0;
}


B题:
B. Caisa and Pylons
time limit per test
1 second
memory limit per test
256 megabytes

Caisa solved the problem with the sugar and now he is on the way back to home.

Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.

Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?

Input

The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1  ≤  hi  ≤  105) representing the heights of the pylons.

Output

Print a single number representing the minimum number of dollars paid by Caisa.

Sample test(s)
Input
5
3 4 3 2 4
Output
4
Input
3
4 4 4
Output
4
Note

In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.

小型模拟,具体就是模拟从第i个阶梯到第i+1个阶梯过程,能量小于0就购买相应小的部分,从0到n-1扫一遍即可,具体代码如下:
#include<cstdio>
int a[100005];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        a[0]=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int en=0;
        long long res=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]-a[i-1]>en)
            {
                res+=a[i]-a[i-1]-en;
                en=0;
            }
            else
                en+=(a[i-1]-a[i]);
        }
        printf("%I64d\n",res);
    }
    return 0;
}

C题:
C. Gargari and Bishops
time limit per test
3 seconds
memory limit per test
256 megabytes

Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prove that he is a genius.

He has a n × n chessboard. Each cell of the chessboard has a number written on it. Gargari wants to place two bishops on the chessboard in such a way that there is no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by one of the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money.

We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it).

Input

The first line contains a single integer n (2 ≤ n ≤ 2000). Each of the next n lines contains n integers aij (0 ≤ aij ≤ 109) — description of the chessboard.

Output

On the first line print the maximal number of dollars Gargari will get. On the next line print four integers: x1, y1, x2, y2 (1 ≤ x1, y1, x2, y2 ≤ n), where xi is the number of the row where the i-th bishop should be placed, yi is the number of the column where the i-th bishop should be placed. Consider rows are numbered from 1 to n from top to bottom, and columns are numbered from 1 to n from left to right.

If there are several optimal solutions, you can print any of them.

Sample test(s)
Input
4
1 1 1 1
2 1 1 0
1 1 1 0
1 0 0 1
Output
12
2 2 3 2

这个题目其实也是比较水,2次贪心即可,对于同一对角线上面元素,例如(x,y),如果他们在同一对角线,则他们x+y为一个定值,另外一个方向x-y为一个定值,所以通过打表先得到每一条对角线的和,然后枚举每一个点,则这个点的对角线总和为row【x+y】+col【x-y】-g【x】【y】,先直接得到最大的那一个放置象的坐标,x1和y1,然后题目要求,对于2次放象位置,不能攻击同一个坐标,那么,我们可以这样想,对于(x1,y1)和(x2,y2),如果他们攻击到了同一点,则他们必然有关系式(x1+y1)%2=(x2+y2)%2,即坐标和具有相同奇偶性,那么我们再次枚举寻找与第一次寻找到的值具有不同奇偶性的坐标,这两个坐标对应的攻击和即为最大攻击,并且输出2个坐标即可。。。具体代码如下:
#include<cstdio>
#include<cstring>
int gg[2005][2005];
long long row[4005],col[4005];
const int ax=2000;
int main()
{
 //   freopen("in.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            scanf("%d",&gg[i][j]);
    }
    int x1=1,y1=1;
    memset(row,0,sizeof(row));
    memset(col,0,sizeof(col));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            row[i+j]+=gg[i][j];
            col[i-j+ax]+=gg[i][j];
        }
    }
    long long maxx1=0,maxx2=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(row[i+j]+col[i-j+ax]-gg[i][j]>=maxx1)
            {
                x1=i;
                y1=j;
                maxx1=row[i+j]+col[i-j+ax]-gg[i][j];
            }
        }
    }
    int pa=(x1+y1)%2;
    int x2=1,y2=2;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(((i+j)%2!=pa)&&row[i+j]+col[i-j+ax]-gg[i][j]>=maxx2)
            {
                x2=i;
                y2=j;
                maxx2=row[i+j]+col[i-j+ax]-gg[i][j];
            }
        }
    }
    printf("%I64d\n",maxx1+maxx2);
    printf("%d %d %d %d\n",x1,y1,x2,y2);
    return 0;
}

D题:
D. Gargari and Permutations
time limit per test
2 seconds
memory limit per test
256 megabytes

Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari?

You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

Input

The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation.

Output

Print the length of the longest common subsequence.

Sample test(s)
Input
4 3
1 4 2 3
4 1 2 3
1 2 4 3
Output
3
Note

The answer for the first test sample is subsequence [1, 2, 3].

这个题目要求k个串的最大公共子序列,这个题目其实解法很多,包括DAG建图求最长路什么的都可以,我的方法使是将问题转化成LCS问题的一个拓展问题,我们可以先打表,就是第i行元素后面对应所有元素全部标记,然后继续套用LCS,在之间判断是否所有的k个串都可以存在这样的序列,然后输出最大的DP【i】就是问题答案。。。算法复杂度O(k*n^2)。。。具体难点就是将2个串的情况扩展到k个串的情况。。。具体代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int a[6][1005],dp[1005];
bool flag[6][1005][1005];
int main()
{
 //   freopen("in.txt","r",stdin);
    int n,k;
    while(cin>>n>>k)
    {
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<n;j++)
                cin>>a[i][j];
        }
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<n;j++)
            {
                for(int p=j+1;p<n;p++)
                {
                    flag[i][a[i][j]][a[i][p]]=true;
                }
            }
        }
        for(int i=0;i<n;i++)
            dp[i]=1;
        int maxx=-1;
        for(int i=n-1;i>=0;i--)
        {
            for(int j=i+1;j<n;j++)
            {
                bool f=false;
                for(int p=0;p<k;p++)
                {
                    if(flag[p][a[0][i]][a[0][j]]==false)
                    {
                        f=true;
                        break;
                    }
                }
                if(f==true)
                    continue;
                if(dp[i]<=dp[j])
                    dp[i]=dp[j]+1;
            }
            if(dp[i]>maxx)
                maxx=dp[i];
        }
        cout<<maxx<<endl;
    }
    return 0;
}


抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值