您的当前位置:首页正文

Pinely Round 4 (Div. 1 + Div. 2)(A~D)

2024-11-08 来源:个人技术集锦

A. 

思路:

签到题,判断每个数前面和后面数的数量能否被2整除,如果都能就可以得到,然后求最大值就行

代码:

#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define N 200010
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k, t, x, y, z;
ll a, b, c, d, mod = 998244353;
ll ans, num, cnt, sum, maxx, minn = 1e9;
ll f1[N], f2[N], dp[1005][1005], an[N];
char ss[1005][1005];
bool flag;
string s, s1, s2;
map<ll, ll>mp;
int main()
{
    void solve();
    ios_base::sync_with_stdio(false);
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
void solve() {
    int n;
    cin >> n;
    vector<int>a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    if (n == 1)
    {
        cout << a[1] << "\n";
        return;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (i & 1)
        {
            ans = max(ans, a[i]);
        }
    }
    cout << ans << "\n";
}

B. 

 思路:

发现除了第一个元素和最后一个元素,其余的都必须同时a[i]和a[i-1]的所有1,然后最后一个元素就是原来的最后一个元素,第一个元素必须是a[1]&b[2],然后遍历一下判断是否正确

代码:

#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define N 200010
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k, t, x, y, z;
ll a, b, c, mod = 998244353;
ll num, cnt, sum, maxx, minn = 1e9;
ll f1[N], f2[N], dp[N], an[N];
char ss[1005][1005];
bool flag;
string s, s1, s2;
map<ll, ll>mp;
int main()
{
    void solve();
    ios_base::sync_with_stdio(false);
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
void solve() {
    cin >> n;
    vector<int>d(n + 1);
    for (int i = 1; i < n; i++) cin >> d[i];
    vector<int>ans(n + 1);

    ans[1] = d[1];
    ans[n] = d[n - 1];
    for (int i = 2; i < n; i++)
    {
        ans[i] = (d[i - 1] | d[i]);
    }
    for (int i = 1; i < n; i++)
    {
        if (d[i] != (ans[i] & ans[i + 1]))
        {
            cout << "-1\n";
            return;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        cout << ans[i] << ' ';
    }
    cout << "\n";
}

 

 

C. 

 思路:

通过操作无法改变任意两个数的相对奇偶性,也就是假如有一奇一偶,那么他们奇偶性一定不同,最后也就无法都化为0.至于剩下的情况,我们不妨可以取他们的max与min,我们要让他们归0,也就一定要让他们变一样,这个问题等价于max=min,于是我们每次都取他们的平均数,这样就可以以指数级别递减。

代码:

#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define N 200010
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k, t, x, y, z;
ll a, b, c, mod = 998244353;
ll num, cnt, sum, maxx, minn = 1e9;
ll f1[N], f2[N], dp[N], an[N];
char ss[1005][1005];
bool flag;
string s, s1, s2;
map<ll, ll>mp;
int main()
{
    void solve();
    ios_base::sync_with_stdio(false);
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> an[i];
    for (int i = 1; i < n; i++) {
        if ((an[i] ^ an[i + 1]) & 1) {
            cout << -1 << endl;
            return;
        }
    }
    ll mid;
    vector<ll>v;
    while (true) {
        sort(an + 1, an + 1 + n);
        mid = (an[1] + an[n]) >> 1;
        if (!mid) break;
        for (int i = 1; i <= n; i++) {
            an[i] = abs(an[i] - mid);
        }
        v.push_back(mid);
    }
    cout << v.size() << endl;
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
    cout << endl;
}

 

D. 

思路:

发现对于 n≥6,其答案都为 4,构造方法:bi=(i−1)mod4+1,由于任意两个相同颜色都对 2 同余,所以异或结果一定是偶数。又因为差了 4,所以不可能是 2。

代码:

#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define N 200010
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k, t, x, y, z;
ll a, b, c, mod = 998244353;
ll num, cnt, sum, maxx, minn = 1e9;
ll f1[N], f2[N], dp[N], an[N];
char ss[1005][1005];
bool flag;
string s, s1, s2;
map<ll, ll>mp;
int main()
{
    void solve();
    ios_base::sync_with_stdio(false);
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
void solve() {
    cin >> n;
    if (n < 6) {
        if (n == 1)
            cout << 1 << endl << "1" << endl;
        if (n == 2)
            cout << 2 << endl << "1 2" << endl;
        if (n == 3)
            cout << 2 << endl << "1 2 2" << endl;
        if (n == 4)
            cout << 3 << endl << "1 2 2 3" << endl;
        if (n == 5)
            cout << 3 << endl << "1 2 2 3 3" << endl;
    }
    else {
        cout << 4 << endl;
        for (int i = 1; i <= n; i++) {
            cout << i % 4 + 1 << " ";
        }
        cout << endl;
    }
}

Top