Educational Codeforces Round 74 (Rated for Div. 2) ABC 题解

A. Prime Subtraction

题意:问你能不能找到一个质数p,使得 b = a - np, 其中a、b给出。

思路:
1.若a - b本身就是质数,那肯定可以。
2.若a - b本身是合数,由唯一分解定理可知该合数可以写成p({_1 ^x}) * p({_2 ^y}) * ... * p({_n ^z}),此时提取一个p来就可以写成np的形式了。
所以除了a - b = 1的情况不行,其他均可以

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        ll x, y;
        cin>>x>>y;
        puts(x-y==1?"NO":"YES");
    }
    return 0;
}

B. Kill 'Em All

题意:一维坐标正半轴上有若干怪物,在x上放个炸弹,能炸死x上的怪物,同时让x左边的怪物位置全部-r,右边的全部+r,怪物到小于等于0的位置时也会死。问最少放多少个炸弹搞定所有怪兽。

思路:贪心一下。既然爆炸点位置右边的会往右偏,那我们就每次都在最右边存在的怪物放炸弹,把前面的怪物往负半轴推就行了。所以用双指针L,R,L代表前L-1个怪物已经被消灭了,R代表目前要炸的那个怪兽编号。每次放一个炸弹就二分一下[L,R]区间内能把多少个数减到小于等于0,同时R--。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <unordered_map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline int read(){ int f = 1; int x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int a[maxn];
unordered_map<int,int> Map;

int main()
{
    FAST;
    cin.tie(0), cout.tie(0);
    int kase;
    scanf("%d",&kase);
    while(kase--)
    {
        Map.clear();
        int n = read(), r = read();
        int p = 0;
        rep(i,1,n)
        {
            int x = read();
            if(!Map[x]) a[++p] = x;
            Map[x] ++;
        }
        if(n==1&&r==1)
        {
            cout<<1<<'
';
            continue;
        }
        sort(a+1,a+1+p);
        int L = 1, R = p;
        int cur = 1;
        int cnt = 0;
        while(L<=R)
        {
            int id = upper_bound(a+L,a+R+1,cur*r) - a;
            id--;
            cnt++;
            cur++;
            L = id+1;
            R--;
        }
        cout<<cnt<<'
';
    }
    return 0;
}

C. Standard Free2play

题意:下台阶,每个高度都有台阶,只不过有的显形有的隐形,并且刚开始给出n个显形的台阶。每次站到一个显形台阶上时,x-1的位置的台阶如果是隐形的就会变成显形的,如果是显形的就会变成隐形的。在每次下降不能超过2个单位的情况下,问最少改变多少个台阶的状态才能到达位置0。

脑补一下下台阶的过程:
1.当前我站的这个位置肯定是显形的(废话
2.如果脚下的台阶本身是隐形的,那交换完状态后显形,就直接下去。
3.如果脚下的台阶是显形的,若x-2的位置还有一个显形的,那就不慌,直接到x-2的位置。但是x-2的位置是隐形的话,那没办法了,只能用一次魔法,到哪里呢?到x-1或x-2都行,因为这两个地方本身就没有台阶,就算x-3有显形台阶,从这两个状态下去也是一样的(照样x-3台阶会消失)。
所以,我们发现对答案有贡献的只有在x+1本身有台阶而x+2没有的情况。所以我们就遍历一遍数组,看a[i]!=a[i+1]的有多少对即可(从n=2开始),相等的时候就i再移动一位。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <unordered_map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline int read(){ int f = 1; int x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int a[maxn];

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        int h = read(), n = read();
        rep(i,1,n) a[i] = read();
        a[n+1] = 0;
        int cnt = 0;
        rep(i,2,n) if(a[i] != a[i+1] + 1) cnt ++; else i++;
        cout<<cnt<<'
';
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Bgwithcode/p/13554719.html