Educational Codeforces Round 95 ABC 题解

A. Buying Torches

题意:合成一个物品需要一个a和一个b,一开始有一个a。现在有下面两种操作:
1.用1个a换x个a。
2.用y个a换1个b。
问你合成k个物品最少需要多少次操作。

思路:水题,先算出一共需要多少个a,然后用这个数除(x-1)上取整得到合成出这么多a需要的次数,再加上k次转化成b的次数即可。

view code
#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 = read(), y = read(), k = read();
        ll all = k*(y+1) - 1;
        ll sum = (all + (x-2))/ (x-1);
        cout<<sum+k<<'
';
    }
    return 0;
}


B. Negative Prefixes

题意:给你个序列。除了有些固定位置不能动,其他位置任意排序。使得最后一次出现前缀和sum[j] < 0的j最小。

思路:贪心,把能随便动的位置上的数从大到小排上去就是最优的。

view code
#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} };

ll a[maxn];
vector<ll> item;
ll ok[maxn];

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        ll n = read();
        item.clear();
        rep(i,1,n) a[i] = read();
        rep(i,1,n) ok[i] = read();
        rep(i,1,n) if(!ok[i]) item.pb(a[i]);
        sort(item.begin(), item.end());
        int cur = item.size() - 1;
        rep(i,1,n) if(!ok[i]) a[i] = item[cur--];
        rep(i,1,n) cout<<a[i]<<' '; cout<<'
';
    }
    return 0;
}


C. Mortal Kombat Tower

题意:给一个01序列。你和你的队友可以取一个数或者两个数。但是队友取1的话要产生1点花费。问最少花费才能全部数取完。

思路:这个题其实够贪心就能过。
首先我们站在队友视角看,当回合是对方时:
1.若当前是0,不管后面有多少个连续0,总有方法使得队友拿最后一个0,然后最先遇到的1让“我”来拿。贪心。
2.若当前是1,那后面如果是0的话,尽量拿。不过这里会有一个wa点,比如 1 0 0 1 1,如果队友一开始拿了1 0,那结果就是2。但是如果他只拿1的话,就能保证后面的连续0最后一个还是队友来拿,最先遇到的1“我”来取。所以这里要给取1 0的时候给悔棋的次数计数。方便后面1少了0的时候拿回来一个。

然后我们站在自己的视角看,当回合是“我”的时候:
1.若当前是1,那看下一个是不是1,是的话把1尽量扫干净。下一个是0的话留给队友。
2.若当前是0,你会发现在两个0以上的时候不管后面有多少个连续0都能让队友取最后一个,然后“我”来拿1。如果只有1个0,就看看前面说的能不能悔棋,可以的话就多出一个0达到前面说的效果。没有的话只能我取走这个唯一的0了。
详见代码注释

view code
#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 = 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 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} };

ll a[maxn];

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        ll n = read();
        rep(i,1,n) a[i] = read();
        int flag = 0;
        int zeros = 0;      //用来悔棋用的
        int turn = 1;       // 回合
        ll ans = 0;
        rep(i,1,n)
        {
            if(flag&&a[i]==0) continue; //flag为真时后面的0都不用管了,肯定能把最后一个0安排给队友
            if(flag) turn=0;        //最后一个0给队友后,当前遇到1,是我的回合
            turn++;
            flag = 0;
            if(turn&1)      //我的回合
            {
                if(a[i]==1)     //若是1
                {
                    if(a[i+1]==1) i++;      //能吃就吃
                }
                else        // 若是0
                {
                    if(a[i+1]==1)       // 如果只有一个0,那就看看能不能悔棋,让队友少吃掉一个0,回退一位
                    {
                        if(zeros>=1) zeros--, flag=1;       //可以的话又可以让队友踩最后一个0
                        else i++;
                    }
                    else  flag=1;       //两个以上0肯定可以让队友踩最后一个0
                }
            }
            else        //队友回合
            {
                if(a[i]==0) flag = 1;       //当前是0,肯定能让队友踩最后一个0
                else
                {
                    if(i+1<=n&&a[i+1]==0) i++, zeros++;     //下一个有0就吃,但是计一下悔棋的步数
                    ans++;
                }
            }
        }
        cout<<ans<<'
';
    }
    return 0;
}


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