西南民族大学 春季 2023 训练赛 2

发布时间 2023-03-30 12:07:07作者: Ke_scholar

被鸽了的课本

A-被鸽了的课本_西南民族大学 春季 2023 训练赛 2 (nowcoder.com)

#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

const int N = 1e5+10;
int n,m,a;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n >> m >> a;
    double x,y;
    x = n + m * 1.0 / 2;
    y = (n + m) * (100 - a) / 100.0; 
    if(y < x) cout << "Through school";
    else cout << "By myself";
     
    
    return 0;
}

小L的序列

B-小L的序列_西南民族大学 春季 2023 训练赛 2 (nowcoder.com)

注意输入的数据会有负数,所以要进行绝对值处理

#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

const int N = 1e5+10;
int n,m,a[N],ans,e[N];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n;
    for(int i = 0 ;i < n ; i++)
     cin >> a[i] ;
    for(int i = 0 ;i < n ;i++)
     {
         int x,one,zero;
         x = one = zero = 0;
         a[i] = abs(a[i]);
         while(a[i])
         {
             if(a[i] % 2 == 0)
              e[x++] = 0;
             else
              e[x++] = 1;
             a[i] /= 2;
         }
         for(int i = 0; i < x; i++)
          {
              if(e[i]) one++;
              else zero ++ ;
          }
          ans += (one > zero) ? 1 : (-1);
     }
     cout << ans << endl;
    
    
    return 0;
}

小Q想撸串

C-小Q想撸串_西南民族大学 春季 2023 训练赛 2 (nowcoder.com)

也可以循环判断NowCoder

#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

const int N = 1e5+10;
int n,m;
string s;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n;
    while(n--)
    {
        int a = 0;
        cin >> s;
        bool flag = false;
        for(int i = 0; i < s.size();i ++)
        {
            if(s[i] == 'N' && !a)
              a++;
            if(s[i] == 'o' && a == 1)
             a++;
            if(s[i] == 'w' && a == 2)
             a++;
            if(s[i] == 'C' && a == 3)
             a++;
            if(s[i] == 'o' && a == 4)
             a++;
            if(s[i] == 'd' && a == 5)
             a++;
            if(s[i] == 'e' && a == 6)
             a++;
            if(s[i] == 'r' && a == 7) 
             {
                 cout << "QAK" << endl;
                 flag = true;
             }
        }
        if(!flag)
         cout << "QIE" << endl;
    } 
    
    
    return 0;
}

兔子的名字

D-兔子的名字_西南民族大学 春季 2023 训练赛 2 (nowcoder.com)

双指针判断子串是否是子序列即可

#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

const int N = 1e5+10;
int n,m;
string name[N],cute[N]; 
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    string s1,s2;
    cin >> n >> m;
    for(int i = 0;i < n;i++)
     cin >> name[i];
    for(int i = 0; i < m ;i++)
     cin >> cute[i] ;
     for(int i= 0 ;i < n; i++)
      {
          int ans = 0;
        for(int j = 0;j < m ;j++)     
          {
            int k , len;
                   for( k = 0, len = 0; k < name[i].size();k++)
                        if(name[i][k] == cute[j][len])
                             len ++;
                        if (len >= cute[j].size())
                            ans++;                   
          }
           cout << ans << endl;
      }    
    return 0;
}

值周

E-值周_西南民族大学 春季 2023 训练赛 2 (nowcoder.com)

如果两个区间有覆盖,则利用差分更新左右端点,如果是独立的两个区间,则用总数减去之前存的区间和,然后将新加入的区间更新为差分的左右端点,因为每次更新后会存在一段未被减去的区间,所以最后需要再减去一次.

代码后有一个样例解析.

#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

const int N = 1e7+10;
int n,m,ans,mp[N];
vector<pair<int,int>> qu;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for(int i = 0; i < m ;i++)
    {
        int x,y;    
        cin >> x >> y;
        qu.push_back({x, y});
    }
    sort(qu.begin(),qu.end());
    int l = qu[0].first, r = qu[0].second;
    ans = n + 1;
    for(auto [i,j] : qu)
    {
        if(i <= r)
            l = min(l, i), r = max(j, r);
        else
        {
            ans -= r - l + 1;
            l = i;
            r = j;
        }
    } 
    ans -= r - l + 1;
    cout << ans << endl;
    
    return 0;
}
// res = 501
// l = 150 , r = 300;
// l = 100 , r = 300;
// res -= 201 // 300 l = 470 , r = 471
// res -= 2;

zn的手环

F-zn的手环_西南民族大学 春季 2023 训练赛 2 (nowcoder.com)

注意他有可能第一天早上9点睡, 第二天早上8点醒,这里要处理一下min的正负,不然最后输出的时间是负数.

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;

const int N = 1e6+10;
typedef long long ll;
int h1,h2,h3,m1,m2,m3;
string str1,str2,str3;
char p1,p2;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    scanf("%d:%d %c.m",&h1, &m1, &p1);
    getchar();
    scanf("%d:%d %c.m",&h2, &m2, &p2);
    getchar();
    scanf("%dh%dmin",&h3, &m3);
//    cout << h1 << ' ' << m1  << ' ' << p1 << endl;
//    cout << h2 << ' ' << m2  << ' ' << p2 << endl; 
    int s, e, all;
    if(p1 == 'p' && p2 == 'p' || p1 == 'a' && p2 == 'a')
        all = h2 * 60 + m2 - (h1 * 60 + m1);
    else 
    {
        if(p1 == 'p')
        all = (h2 * 60) + m2 + (12 * 60 - (h1 * 60 + m1));
        else
         all = h1 * 60 + m1 + (12 * 60 - (h2 * 60 + m2)); 
    }
    //cout << all << endl;
        if(all == h3 * 60 + m3)
         printf("YES\n");
        else
        {
            int hall = (all / 60 > 0) ? all / 60 : all / 60 + 24;
            int mall = all % 60;
            if(mall < 0) 
            {
                hall--;
                mall += 60;
            }
         printf("NO\n");
         printf("%dh%dmin\n",hall ,mall );
        }      
    return 0;
}

兔子的逆序对

H-兔子的逆序对_西南民族大学 春季 2023 训练赛 2 (nowcoder.com)

利用逆序对的性质,每有两个数对换那么原数组的逆序对的奇偶性就会变化,而每次对换可以是任意两个数,那我们只需要简单将前后两个数对换,即区间内的数 + 1再除以2就是对换的次数.若原逆序对是奇数个,对换的次数也是奇数个,那说明此时他的逆序对就是偶数个.

那这道题就很好做了,先判断原逆序对是否为奇数,奇数得到话加上奇数次对换就变成了偶数个,那就更新原逆序对个数++,反之原逆序对个数为偶数,对换次数也是偶数,那最后逆序对个数还是偶数,就不用修改原逆序对的奇偶性.

#include<bits/stdc++.h>
//#define int long long
#define endl '\n'

using namespace std;

const int N = 1e5+10;
int n,m,a[N];
string s;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n;
    for(int i = 1 ; i <= n ;i++)
     cin >> a[i];
     int ans = 0;
        for(int i = 1 ;i <= n - 1; i ++) 
        {
            for(int j = i + 1; j <= n ;j ++)
             {
                 if(a[i] > a[j]) ans++;
             }
        }     
    cin >> m ; 
    while(m--)
    {
        int l,r;
        cin >> l >> r;
        int res = (r - l + 1) >> 1;
        if(ans & 1)
        {
            if(res & 1)
            {
                cout << "like" << endl;
                ans ++;
            }
            else
             cout << "dislike" << endl;
         } 
        else
        {
            if(res & 1)
            {
             cout << "dislike" << endl;
             ans ++;                
            }
            else
             cout << "like" << endl;
        }
    }     
    return 0;
}