2022CCPC桂林补题A-B

190 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第30天,点击查看活动详情
本文已参与「新人创作礼」活动,一 起开启掘金创作之路

简单题A,M

A.Lily

— Assignment Outerspace, 1960Everything that has a beginning has an ending. My journey has been reaching its ending, and I've been ready to say goodbye to my yesterday. But you, my dear friend, your journey is still thriving here at the 2022 CCPC Guilin Site. We sincerely hope you find a brand new milestone here, and forge ahead in the future with your love and passion.
Lily means a kind of beautiful flower. She usually only blooms once a year, so it could be very precious if you see a lily blooming. However, she is highly toxic to cats, so you must be aware of keeping curious cats away from lovely lilies.
You have nn grids of soil land in a row, for 11 to nn, some with lilies blooming. We don't want to hurt lilies, as well as cats. You can put some cat food on the grids, but for any grid ii with cat food, grids with indices falling in the range [i1,i+1][i - 1, i + 1] must not contain lily flowers. You love cats and lilies, so you want to maximize the number of grids having cat food.
Design a plan to fulfill the above requirements.

中文大意

如果这个点的旁边一个单位没有L的话就可以放C

  const int N = 2e5 + 10;
  char s[N];
  void solve()
  {
  int n; cin >> n;
  cin >> s + 1;
  for(int i = 1; i <= n; i++) {
  if(s[i-1] != 'L' && s[i + 1] !='L' && s[i] != 'L') s[i] = 'C';
  }
  rep(i,n) cout << s[i];
  }

M.Youth Finale

Finales are born to be exciting. Performers play hard to draw audiences' attention and then take a perfect curtain call. As the last problem and the finale of the problem set, however, we want you to recall a simple algorithm. Like me, it may be the first algorithm you've learned, called Bubble Sort.
void bubble_sort(int a[], int n) { // 0-based, sort from lowest to highest for (int i = 1; i < n; i++) { for (int j = 0; j < n - i; j++) { if (a[j] > a[j + 1]) { swap(a[j], a[j + 1]); } } // after i-th inner iteration, a[n - i] is correct }}Given a permutation of length nn, as you might know, Bubble Sort runs in Ω(n2)\Omega (n ^ 2) in the worst case. It's quite a traditional idea to count the number of calls of "swap" in the algorithm. As you are stronger now, you want to count that number in a dynamic permutation with the following events that might happen:
Reverse the permutation, meaning that the permutation is replaced with p={pn,pn1,,p2,p1}. p' = \{p_n, p_{n - 1}, \dots, p_2, p_1\}. Shift the permutation to the left by 11, meaning that the permutation is replaced with p={p2,p3,,pn,p1}. p' = \{p_2, p_3, \dots, p_n, p_1\}. All you need to do is to output the number of "swap" that would be called if we sort the permutation with the above Bubble Sort code after each operation.

中文大意

就是我们可以执行两种操作一种是将整个数组翻转另一种是将数组的开头放到末尾。
问在操作一次过后如果对当前数组使用冒泡排序需要交换多少次

解法

我们发现一开始交换的次数就是我们这个数组中逆序对的数目所以这个数组一共可以构成(n1)n/2(n-1)*n/2个对 在我们翻转过后正序对和逆序对的数量会交换,然后我们发现在我们另一个操作的时候会发现我们操作的是第一个数所以他对答案的影响就是nxx+1n-x-x+1

const int N = 1e6+ 10;
int st[N];
int a[N];
char s[N];
template<class T>
struct BIT
{
    int lowbit(int x){ return x & -x;}
    int tree[N];
    int size;
    void resize(int x) {size = x;}
    void add(int x,T k)
    {
    for(x; x <= size; x += lowbit(x)) tree[x] += k;
    }
    int calc(int x)
    {
    T res = 0;
    for(x; x ; x -= lowbit(x)) res += tree[x];
    return res;
    }
};
BIT<int> c;
void solve()
{
    int n,m; cin >> n >> m;
    c.resize(n);
    int res = 0;
    rep(i,n) cin >> a[i];
    rep(i,n) {
        st[a[i]] = c.calc(n+1-a[i]);
        res += st[a[i]];
        c.add(n+1-a[i],1);
    }
    rep(i,m) cin >> s[i];
    cout << res << endl;
    int ans = res;
    int f = 1;
    int cnt = 1;
    for(int i = 1; i <= m; i++) {
        int x = a[cnt];
        if(s[i] == 'S') {
          if(f) {
             ans -= x - 1;
             ans += n - x;
             cnt ++;
             if(cnt == n + 1) cnt = 1;
          }else {
             ans -= x - 1;
             ans += n - x;
             cnt --;
             if(cnt == 0) cnt = n;
          }
        }else {
            ans = (n - 1) * n / 2 - ans;
            if(f) {
                cnt --;
                if(cnt == 0) cnt = n;
            }else {
                cnt ++;
                if(cnt == n + 1) cnt = 1;
            }
            f ^= 1;
        }
        while(ans < 0) ans += 10;
        ans %= 10;
        cout << ans % 10;
    }
}