Educational Codeforces Round 74 (Rated for Div. 2)

A - Prime Subtraction

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 100 + 5 ;
const int maxM = 2e5 + 5;
 
ll x, y, dif;
 
int main()
{
    int t; scanf("%d", &t);
    while(t -- )
    {
        scanf("%lld%lld", &x, &y);
        dif = x - y;
        if(dif == 1 )
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}

B - Kill `Em All

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define P(x) x < 0 ? 0 : x
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 1e5 + 5 ;
const int maxM = 2e5 + 5;

int n, r;
int a[maxN];

int main()
{
    int q; scanf("%d", &q);
    while(q -- )
    {
        scanf("%d%d", &n, &r);
        for(int i = 0; i< n ; i ++ )
            scanf("%d", &a[i]);
        sort(a, a + n);
        n = unique(a, a + n) - a;
        int ans = 0;
        for(int i = n - 1; i >= 0 ; i -- )
        {
            if(a[i] - ans * r <= 0)
                continue;
            ans ++;
        }
        printf("%d\n", ans);
    }
    return 0;
}

C - Standard Free2play

题意:

  • 高度为 h 的悬崖,有 n 个台阶是初始状态在外面可以着陆的。我们从第一节台阶(也就是高度为 h 的那个台阶)开始跳,每次跳的高度不能超过 2 。当然我们可以使用水晶改变任何一个高度的台阶状态。
  • 如果我们从高度为 x 的台阶跳, x 这个台阶会消失,并且高度为 x - 1 的这个台阶会改变它的原来状态(如果有,就变成没有;如果没有,就变成有)。
  • 问:到达地面最少使用的水晶数。

思路:

假设我们在 高度为 x 的台阶上往下跳

  1. 如果 x - 1不存在台阶,那么我们就可以直接跳。于是我们跳到了x - 1这个台阶上。
  2. 如果 x - 1存在台阶,那么如果x - 2存在台阶,那么还是可以直接跳。于是我们跳到了x - 2这个台阶上。
  3. 如果 x - 1存在台阶,但是x - 2不存在台阶,那么这个时候我们就必须使用水晶改变 x - 2的状态,使这个高度出现台阶。于是我们跳到了x - 2这个台阶上,并且使用了一颗水晶。

所以我们只需要这样做:

  1. 遍历一遍初始状态下的台阶。 每两个台阶中间的高度是不存在台阶的,所以就是上述所说的情况1,可以直接跳不管。
  2. 我们最开始一定会顺利地跳到:第二个台阶的上面一个高度的那个不存在的台阶,设为tmp。且这时我们跑到的第 i 个台阶, 也就是tmp - 1,是一定存在的。
  3. 从tmp开始考虑上述的情况2和情况3
  4. 如果是情况2,那么我们就直接跳到了x - 2这个台阶上,所以x - 2这个台阶就变为了悬崖顶的第一个台阶,再找它下面那个台阶(为第i + 2个台阶)的上面一个高度的不存在的台阶,接着考虑情况2,3
  5. 如果是情况3,那么我们就是跳到了用水晶改变得来的这个x - 2台阶,所以x - 2这个台阶就变为了悬崖顶的第一个台阶,再找它下面那个台阶(为第 i + 1个台阶)的上面一个高度的不存在的台阶,接着考虑情况2,3

注意:

  • 因为高度有1e9,所以不能用数组判断情况2、3的时候不能直接用标记数组,会RE,可以用set,然后用find()函数判断是不是存在

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define P(x) x < 0 ? 0 : x
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 2e5 + 5 ;
const int maxM = 2e5 + 5;

int h, n;
int a[maxN] ;
int main()
{
    int q; scanf("%d", &q);
    while(q -- )
    {
        scanf("%d%d", &h, &n);
        set<int>st;
        for(int i = 0; i < n ; i ++ )
        {
            scanf("%d", &a[i]);
            st.insert(a[i]);
        }
        a[n] = 0;
        st.insert(a[n]);
        int ans = 0;
        for(int i = 1; i < n ;  )
        {
            int tmp = a[i] + 1;//第二个台阶的上一节不存在的台阶
            if(st.find(tmp - 2) != st.end())
                i += 2;
            else
            {
                i ++ ;
                ans ++ ;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(CodeForces)