其他分享
首页 > 其他分享> > 【Day2】一名菜鸟ACMer的暑假训练

【Day2】一名菜鸟ACMer的暑假训练

作者:互联网

【Day2】一名菜鸟ACMer的暑假训练

(只有半天的训练)

CF构造题1200

B. Sorted Adjacent Differences

https://codeforces.com/problemset/problem/1339/B
题意:
构造一个满足
这一条件的序列。
解法:
先sort,然后一左一右取(这样就保证了绝对值是递减的),最后把序列翻转过来即可
Code:

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
int a[N];

void solve () {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i ++)
        cin >> a[i];

    sort (a + 1, a + n + 1);

    vector <int> v;
    int i, j;
    for (i = 1, j = n; i < j; i ++, j --)
        v.push_back (a[i]), v.push_back (a[j]);
    if (i == j) v.push_back (a[i]);
    reverse (v.begin (), v.end ());

    for (auto i : v)    cout << i << ' ';
    cout << endl;
    
}

int main () {
    int t;
    cin >> t;
    while (t --) {
        solve ();
    }
}


//构造序列,满足绝对值递增
//先sort,然后一左一右取(这样就保证了绝对值是递减的)
//最后把序列翻转过来即可
//一大一小

C. Challenging Cliffs

https://codeforces.com/problemset/problem/1537/C
题意:
对于\(a_i,a_{i+1}\),若\(a_i<=a_{i+1}\),则difficulty + 1
构造\(|a_1-a_n|\)最小的序列的前提下,difficulty最大的序列
解法:
(激动的手舞足蹈!!因为我想出来了)
先sort
找到绝对值之差最小的两个,拦腰砍断,前后换位
eg. 1 2 2 4 -> 2 4 1 2
注意特判只有两个的情况

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5;
int a[N];

void solve () {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i ++)   cin >> a[i];
    sort (a + 1, a + n + 1);

    if (n == 2) {
        cout << a[1] << ' ' << a[2] << endl;
        return ;
    }

    int minn = a[n] - a[1] + 1, st = -1;

    for (int i = 2; i <= n; i ++) {
        int dx = a[i] - a[i - 1];
        if (dx < minn)  minn = dx, st = i;
    }

    vector <int> v;
    for (int i = st; i <= n; i ++)  v.push_back (a[i]);
    for (int i = 1; i < st; i ++)   v.push_back (a[i]);

    for (auto i : v)    cout << i << ' ';
    cout << endl;
}

int main () {
    int t;
    cin >> t;
    while (t --) {
        solve ();
    }
}
//相等和增大 + 1
//求首尾之差最小的前提下,分数最大

//先sort
//找到绝对值之差最小的两个,拦腰砍断,前后换位
//eg. 1 2 2 4 -> 2 4 1 2

B1. Palindrome Game (easy version)

https://codeforces.com/problemset/problem/1527/B1
题意:
给定一个回文串
可以有两种操作(A先手):

  1. si: 0->1, cost ++
  2. reverse s, cost不变(适用:当前非回文,对手上次操作不是2)
    全1->over, cost最少的赢

解法:
最开始A只能op1,cost ++
要想赢肯定会尽可能reverse,能2则2
猜一波,偶(>2)B奇A
原因:
0为奇数个的话,最中间的肯定是0
那么A就可以先把最中间的变成1,这样就还是回文串
接着模拟后面的过程,就会发现B输了

#include <bits/stdc++.h>

using namespace std;

void solve () {
    int n;
    string s;
    cin >> n >> s;
    int cnt = 0;
    for (int i = 0; i < s.size (); i ++)
        if (s[i] == '0')    cnt ++;
    if (cnt <= 2 || cnt % 2 == 0)    cout << "BOB\n";
    else if (cnt & 1)   cout << "ALICE\n";
    //else    cout << "DRAW\n"; //没有平手。。泪目
}

int main () {
    int t;
    cin >> t;
    while (t --) {
        solve ();
    }
}

//给定一个回文串
//可以有两种操作(A先手):
//1. si: 0->1, cost ++
//2. reverse s, cost不变(适用:当前非回文,对手上次操作不是2)
//全1->over, cost最少的赢

//最开始A只能op1,cost ++
//要想赢肯定会尽可能reverse,能2则2
//猜一波,偶(>2)B奇A
//原因:
//0为奇数个的话,最中间的肯定是0
//那么A就可以先把最中间的变成1,这样就还是回文串
//接着模拟后面的过程,就会发现B输了

C1. k-LCM (easy version)

https://codeforces.com/problemset/problem/1497/C1
题意:
选3个数
sum = n, lcm <= n/2
解法:
多写几项,找到规律:

1. n为奇:          1   n/2   n/2
2.n%4==0:         n/4  n/4   n/2  
3.n%2==0 && n%4:   2  n/2-1  n/2-1
#include <bits/stdc++.h>

using namespace std;

void solve () {
    int n, k;
    cin >> n >> k;
    if (n & 1) {
        cout << "1 " << n/2 << ' ' << n/2 << endl;
        return ;
    }
    n /= 2;
    if (n & 1)  cout << "2 " << n-1 << ' ' << n-1 << endl;
    else    cout << n << ' ' << n/2 << ' ' << n/2 << endl;

}

int main () {
    int t;
    cin >> t;
    while (t --) {
        solve ();
    }
}

//选3个数
//sum = n, lcm <= n/2

//多写几项,找到规律:
//1. n为奇:          1   n/2   n/2
//2.n%4==0:         n/4  n/4   n/2  
//3.n%2==0 && n%4:   2  n/2-1  n/2-1

A了构造题的感觉真爽

标签:sort,int,菜鸟,Day2,++,ACMer,cost,solve,回文
来源: https://www.cnblogs.com/CTing/p/16443562.html