1. 程式人生 > 實用技巧 >Educational Codeforces Round 95(A-C題解)

Educational Codeforces Round 95(A-C題解)

A. Buying Torches

題目:http://codeforces.com/contest/1418/problem/A

題解:計算一個公式:1+n*(x-1)=(y+1)*k,求滿足該條件的n,很簡單的一道題

程式碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t,x,y,k;
int main()
{
    ll i,j;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>k;
        ll n;
        n
=((y+1)*k-1)/(x-1)+(((y+1)*k-1)%(x-1)==0?0:1); cout<<n+k<<endl; } return 0; }

B. Negative Prefixes

題目:http://codeforces.com/contest/1418/problem/B

題解:將可變的數單獨拿出來進行排序,然後按照從大到小進行插入即可。因為本題求得的是最小的K,而K是重排後的序列中字首和j最大的小於零的那個值。因此我們既然要滿足K的最小,就要將可變的值從大到小依次插入,則求出的即為最小的。

程式碼:

#include<bits/stdc++.h>
using
namespace std; typedef long long ll; const int maxn=1e5+10; ll t,n,a[maxn]; ll f[maxn]; ll b[maxn]; int main() { ll i,j; cin>>t; while(t--) { cin>>n; ll len=0; for(i=1;i<=n;i++) { cin>>a[i]; } for(i=1;i<=n;i++) { cin
>>f[i]; if(f[i]==0) b[len++]=a[i]; } ll cnt=len-1; sort(b,b+len); for(i=1;i<=n;i++) { if(f[i]==1) cout<<a[i]<<" "; else cout<<b[cnt--]<<" "; } cout<<endl; } return 0; }

C. Mortal Kombat Tower

題目:http://codeforces.com/contest/1418/problem/C

題解:首先第一位要進行特判,若為1,則ans++;

還有111這種情況,我們可以幹掉2個怪物,而朋友必須消滅一個,因此這類情況ans++

其餘的,000,001,010,100,110,101,011,我們的朋友都是可以避免的,不用消耗

最終的答案就是:特判第一位+【2,n】位中有多少個111,就加多少個。

程式碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
ll t,n,a[maxn];
int main()
{
    ll i,j;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(i=1;i<=n;i++)
            cin>>a[i];
        ll cnt=0;
        ll ans=0;
        if(a[1]==1)
            ans++;
        for(i=2;i<=n;i++)
        {
            if(a[i]==1)
                cnt++;
            else
                cnt=0;
            if(cnt==3)
            {
                ans++;
                cnt=0;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}