11月26日训练

A.Make Even

题意:Polycarp有一个整数𝑛不包含数字0的。他可以使用他的数字执行以下操作数次(可能为零):
颠倒长度前缀𝑙换句话说,𝑙 最左边的数字)𝑛. 因此,最左边的数字与𝑙-左起第个数字,左起第二个数字与(𝑙−1) -左th等,例如,如果𝑛=123456789和𝑙=5的新值𝑛 将是543216789.
请注意,对于不同的操作𝑙可能会有所不同。号码𝑙 可以等于数字的长度𝑛 —在本例中,整数𝑛相反。

Polycarp喜欢偶数。因此,他想使他的数字相等。同时,Polycarp非常不耐烦。他想做尽可能少的操作。

帮助Polycarp。确定他需要使用该号码执行的最小操作数𝑛使其均衡或确定这是不可能的。
你需要回答𝑡组独立测试用例。

题意:对于每一个数,
1.他的本身偶数的话,就是偶数
2.他的第一位是偶数,最后一位是奇数的话,需要反转一次成为偶数
3.对于前两个都不符合的数,如果有任意一个数是偶数,需要反转三次成为偶数
4.全是奇数的序列不能成为偶数

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <fstream>
#include <climits>

using namespace std;
typedef long long ll;

#define pi acos(-1.0)

#define oula                                                              \
    bool notprime[1000010];                                               \
    int primes[1000010];                                                  \
    void get_prime()                                                      \
    {                                                                     \
        notprime[1] = true;                                               \
        for (int i = 2; i < 1000010; ++i)                                 \
            if (!notprime[i])                                             \
            {                                                             \
                primes[++primes[0]] = i;                                  \
                for (long long j = (long long)i * i; j < 1000010; j += i) \
                    notprime[j] = true;                                   \
            }                                                             \
    };

#define ksm                           \
    inline ll qpow(ll x, ll a)        \
    {                                 \
        ll ret = 1, k = x;            \
        for (; a; a >>= 1, k = k * k) \
            if (a & 1)                \
                ret = ret * k;        \
        return ret;                   \
    }

#define gcdxy                     \
    ll gcd(ll a, ll b)            \
    {                             \
        if (a < b)                \
            swap(a, b);           \
        if (b == 0)               \
            return a;             \
        else                      \
            return gcd(a % b, b); \
    }

#define fread                           \
    inline ll read()                    \
    {                                   \
        char ch = getchar();            \
        ll x = 0, f = 1;                \
        while (ch < '0' || ch > '9')    \
        {                               \
            if (ch == ' - ')            \
                f = -1;                 \
            ch = getchar();             \
        }                               \
        while ('0' <= ch && ch <= '9')  \
        {                               \
            x = x * 10 + int(ch - '0'); \
            ch = getchar();             \
        }                               \
        return x * f;                   \
    }

#define xor_                    \
    ll xor_n(ll n)              \
    {                           \
        ll t = n & 3;           \
        if (t & 1)              \
            return t / 2ll ^ 1; \
        return t / 2ll ^ n;     \
    }

#define exgcd_                         \
    ll exgcd(ll a, ll b, ll &x, ll &y) \
    {                                  \
        if (b == 0)                    \
        {                              \
            x = 1;                     \
            y = 0;                     \
            return a;                  \
        }                              \
        ll r = exgcd(b, a % b, x, y);  \
        ll t = x;                      \
        x = y;                         \
        y = t - a / b * y;             \
        return r;                      \
    }
#define mksm                                \
    int mod = 1e9 + 7;                      \
    inline ll mqpow(ll x, ll a)             \
    {                                       \
        ll ret = 1, k = x;                  \
        for (; a; a >>= 1, k = k * k % mod) \
            if (a & 1)                      \
                ret = ret * k % mod;        \
        return ret;                         \
    }

//oula
ksm

gcdxy

fread

xor_

exgcd_

mksm


ll abs_(ll x) {
    return x < 0 ? -x : x;
}


//int find(int x)
//{
//    if (a[x] != x) a[x] = find(a[x]);
//    return a[x];
//}
//
//void join(int x, int y) {
//    a[find(x)] = find(y);
//}
int n, m, high, low;
int a[10000010];

bool judge(int mid) {
    int ans = 1, tmp = a[0];
    for (int i = 1; i < n; i++) {
        if (a[i] - tmp >= mid) {
            tmp = a[i];
            ans++;
        }
    }
    if (ans >= m)
        return true;
    else
        return false;
}

int main(void) {
    // ifstream cin;
    // fstream cout;
    // cin.open("input.txt", ios::in);
    // cout.open("ans.txt", ios::out);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        if (s[s.size() - 1] % 2 == 0) {
            cout << "0" << endl;
        }
        else if(s[0] % 2 == 0){
            cout << "1" << endl;
        }
        else{
            int flag = 0;
            for(int i = 0; i < s.size(); i++)
            {
                if(s[i]%2 == 0)
                {
                    flag = 1;
                    break;
                }
            }
            if(flag)
                cout << 2 << endl;
            else
                cout << -1 << endl;
        }
    }
}

/*

4
3
1 3 4
5
1 2 5 7 4
1
1
3
69 6969 696969

-1 1  4  0 -5  1  8 0 -9 1  12  0
1  2  3  4  5  6  7 8 9  10 11 12

3 1 -2 2
1 2  3 4

2 0 -3 1  6 0 -7 1
1 2 3  4  5 6 7 8

3 2 -4 -2 0
7 6 2 4

5 4 2
*/

B. Team Composition: Programmers and Mathematicians

题意:所有Berland团队编程比赛将很快举行。
今年,四人小组被允许参加。

有𝑎 程序员和𝑏伯兰州立大学的数学家。如果:每个团队必须由4名学生组成,由4名数学家或4名程序员组成的团队不太可能表现良好,因此决定不组建这样的团队,那么最多可以组建多少个团队。

因此,每个团队必须至少有一名程序员和一名数学家。

打印所需的最大团队数。每个人只能是一个团队的成员。

题解:对于数学家和程序员来说,贡献都是一样的,只需要判断a是否大于3b和b是否大于3a之后,直接除4则是最大分配方案

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <fstream>
#include <climits>

using namespace std;
typedef long long ll;

#define pi acos(-1.0)

#define oula                                                              \
    bool notprime[1000010];                                               \
    int primes[1000010];                                                  \
    void get_prime()                                                      \
    {                                                                     \
        notprime[1] = true;                                               \
        for (int i = 2; i < 1000010; ++i)                                 \
            if (!notprime[i])                                             \
            {                                                             \
                primes[++primes[0]] = i;                                  \
                for (long long j = (long long)i * i; j < 1000010; j += i) \
                    notprime[j] = true;                                   \
            }                                                             \
    };

#define ksm                           \
    inline ll qpow(ll x, ll a)        \
    {                                 \
        ll ret = 1, k = x;            \
        for (; a; a >>= 1, k = k * k) \
            if (a & 1)                \
                ret = ret * k;        \
        return ret;                   \
    }

#define gcdxy                     \
    ll gcd(ll a, ll b)            \
    {                             \
        if (a < b)                \
            swap(a, b);           \
        if (b == 0)               \
            return a;             \
        else                      \
            return gcd(a % b, b); \
    }

#define fread                           \
    inline ll read()                    \
    {                                   \
        char ch = getchar();            \
        ll x = 0, f = 1;                \
        while (ch < '0' || ch > '9')    \
        {                               \
            if (ch == ' - ')            \
                f = -1;                 \
            ch = getchar();             \
        }                               \
        while ('0' <= ch && ch <= '9')  \
        {                               \
            x = x * 10 + int(ch - '0'); \
            ch = getchar();             \
        }                               \
        return x * f;                   \
    }

#define xor_                    \
    ll xor_n(ll n)              \
    {                           \
        ll t = n & 3;           \
        if (t & 1)              \
            return t / 2ll ^ 1; \
        return t / 2ll ^ n;     \
    }

#define exgcd_                         \
    ll exgcd(ll a, ll b, ll &x, ll &y) \
    {                                  \
        if (b == 0)                    \
        {                              \
            x = 1;                     \
            y = 0;                     \
            return a;                  \
        }                              \
        ll r = exgcd(b, a % b, x, y);  \
        ll t = x;                      \
        x = y;                         \
        y = t - a / b * y;             \
        return r;                      \
    }
#define mksm                                \
    int mod = 1e9 + 7;                      \
    inline ll mqpow(ll x, ll a)             \
    {                                       \
        ll ret = 1, k = x;                  \
        for (; a; a >>= 1, k = k * k % mod) \
            if (a & 1)                      \
                ret = ret * k % mod;        \
        return ret;                         \
    }

//oula
ksm

gcdxy

fread

xor_

exgcd_

mksm


ll abs_(ll x) {
    return x < 0 ? -x : x;
}


//int find(int x)
//{
//    if (a[x] != x) a[x] = find(a[x]);
//    return a[x];
//}
//
//void join(int x, int y) {
//    a[find(x)] = find(y);
//}

int main(void) {
    // ifstream cin;
    // fstream cout;
    // cin.open("input.txt", ios::in);
    // cout.open("ans.txt", ios::out);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        int a, b;
        cin >> a >> b;
        cout << min(min(a, b), (a + b) / 4) << endl;
    }
    return 0;
}

/*
1 4 2 3 5

1 4 3 2 5

1 2 3 4 5

1 2 3 4 5

1 3 5 2 4

1 2 3 4 5

1 3 4 2 5

1 2 5 3 4




4
3
1 3 4
5
1 2 5 7 4
1
1
3
69 6969 696969

-1 1  4  0 -5  1  8 0 -9 1  12  0
1  2  3  4  5  6  7 8 9  10 11 12

3 1 -2 2
1 2  3 4

2 0 -3 1  6 0 -7 1
1 2 3  4  5 6 7 8

3 2 -4 -2 0
7 6 2 4

5 4 2
*/

C - Polycarp Recovers the Permutation

Polycarp在白板上写了一个数组𝑝 长度𝑛, 这是从1到1的数字排列𝑛. 换句话说,在𝑝 每个数字从1到𝑛 只发生一次。

他还准备了一个结果数组𝑎, 它最初为空(即,它的长度为0)。

在那之后,他确实做到了𝑛 步骤。

每一步都是这样的:

看看最左边和最右边的元素𝑝, 然后从两个中选择较小的一个。

如果您选择了𝑝, 将其附加到𝑎; 否则,如果选择了𝑝, 将其附加到𝑎.

拾取的元素将从中删除𝑝.

注意,在最后一步,𝑝

长度为1,其最小元素为最左侧和最右侧。在这种情况下,Polycarp可以选择最小元素所扮演的角色。换句话说,可以将此元素添加到𝑎 左侧和右侧(由Polycarp自行决定)。

让我们看一个例子。允许𝑛=4.𝑝=[3,1,4,2]. 开始𝑎=[].

然后:

在第一步中,最小值在右边(值为2),因此在这一步之后,𝑝=[3,1,4]和𝑎=[2] (他在右边加了值2)。

在第二步中,最小值在左边(值为3),因此在这一步之后,𝑝=[1,4]和𝑎=[3,2](他在左边加了值3)。

在第三步中,最小值在左边(值为1),因此在这一步之后,𝑝=[4] 及𝑎=[1,3,2](他将值1添加到左侧)。

在第四步中,最小值为左侧和右侧(该值为4).假设Polycarp选择了正确的选项。经过这一步,,𝑝=[]和𝑎=[1,3,2,4](他在右边加上了值4)。

因此,一个可能的值𝑎 之后𝑛 步骤可能是𝑎=[1,3,2,4].

您将获得结果数组的最终值𝑎. 找到任何可能的初始值𝑝 这可能导致给定的结果𝑎, 或者确定没有解决方案。

题解:只有数组最大值在左右端点才可以去来回操作的结果一样,对于每个数组即判断左右端点是否是数组最大值即可;
注意:有可能有大于n的值,需要x掉
最后反向输出就好了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <fstream>
#include <climits>

using namespace std;
typedef long long ll;

#define pi acos(-1.0)

#define oula                                                              \
    bool notprime[1000010];                                               \
    int primes[1000010];                                                  \
    void get_prime()                                                      \
    {                                                                     \
        notprime[1] = true;                                               \
        for (int i = 2; i < 1000010; ++i)                                 \
            if (!notprime[i])                                             \
            {                                                             \
                primes[++primes[0]] = i;                                  \
                for (long long j = (long long)i * i; j < 1000010; j += i) \
                    notprime[j] = true;                                   \
            }                                                             \
    };

#define ksm                           \
    inline ll qpow(ll x, ll a)        \
    {                                 \
        ll ret = 1, k = x;            \
        for (; a; a >>= 1, k = k * k) \
            if (a & 1)                \
                ret = ret * k;        \
        return ret;                   \
    }

#define gcdxy                     \
    ll gcd(ll a, ll b)            \
    {                             \
        if (a < b)                \
            swap(a, b);           \
        if (b == 0)               \
            return a;             \
        else                      \
            return gcd(a % b, b); \
    }

#define fread                           \
    inline ll read()                    \
    {                                   \
        char ch = getchar();            \
        ll x = 0, f = 1;                \
        while (ch < '0' || ch > '9')    \
        {                               \
            if (ch == ' - ')            \
                f = -1;                 \
            ch = getchar();             \
        }                               \
        while ('0' <= ch && ch <= '9')  \
        {                               \
            x = x * 10 + int(ch - '0'); \
            ch = getchar();             \
        }                               \
        return x * f;                   \
    }

#define xor_                    \
    ll xor_n(ll n)              \
    {                           \
        ll t = n & 3;           \
        if (t & 1)              \
            return t / 2ll ^ 1; \
        return t / 2ll ^ n;     \
    }

#define exgcd_                         \
    ll exgcd(ll a, ll b, ll &x, ll &y) \
    {                                  \
        if (b == 0)                    \
        {                              \
            x = 1;                     \
            y = 0;                     \
            return a;                  \
        }                              \
        ll r = exgcd(b, a % b, x, y);  \
        ll t = x;                      \
        x = y;                         \
        y = t - a / b * y;             \
        return r;                      \
    }
#define mksm                                \
    int mod = 1e9 + 7;                      \
    inline ll mqpow(ll x, ll a)             \
    {                                       \
        ll ret = 1, k = x;                  \
        for (; a; a >>= 1, k = k * k % mod) \
            if (a & 1)                      \
                ret = ret * k % mod;        \
        return ret;                         \
    }

//oula
ksm

gcdxy

fread

xor_

exgcd_

mksm


ll abs_(ll x) {
    return x < 0 ? -x : x;
}


//int find(int x)
//{
//    if (a[x] != x) a[x] = find(a[x]);
//    return a[x];
//}
//
//void join(int x, int y) {
//    a[find(x)] = find(y);
//}
int a[1000010];

int main(void) {
    // ifstream cin;
    // fstream cout;
    // cin.open("input.txt", ios::in);
    // cout.open("ans.txt", ios::out);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        if (a[0] != n && a[n - 1] != n) {
            cout << -1 << endl;
            continue;
        }
        reverse(a,a+n);
        for(int i = 0; i < n ;i++)
        {
            cout << a[i] << " ";
        }
        cout << endl;
    }
}

/*
1 4 2 3 5

1 4 3 2 5

1 2 3 4 5

1 2 3 4 5

1 3 5 2 4

1 2 3 4 5

1 3 4 2 5

1 2 5 3 4




4
3
1 3 4
5
1 2 5 7 4
1
1
3
69 6969 696969

-1 1  4  0 -5  1  8 0 -9 1  12  0
1  2  3  4  5  6  7 8 9  10 11 12

3 1 -2 2
1 2  3 4

2 0 -3 1  6 0 -7 1
1 2 3  4  5 6 7 8

3 2 -4 -2 0
7 6 2 4

5 4 2
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值