2021 CSP-J 总结

· · 个人记录

T1 分糖果

代码

#include <bits/stdc++.h>
using namespace std;
int n, l, r;
int main()
{
 cin >> n >> l >> r;
 if (r - l /*可以拿的糖果范围*/ >= n - 1 /*最多能拿到的奖励数量*/) // 如果范围更大,那么一定可以得到最多的奖励
  cout << n - 1;
 else if (l % n + (r - l) >= n - 1) // 如果恰好能得到最多奖励的糖果数量在范围内
  cout << n - 1;
 else
  cout << l % n + (r - l);
}

思路

容易发现,你获得的奖励数量就是 拿了的糖果数量 \mod n,而 \mod n 的结果一定 \in \{0, 1, \cdots, n-1\},且连续增加,所以只要 L-R \geq n-1,答案就是 n-1。否则,如果 L \mod n + (R-L) \geq n-1,即在 L-R 的范围内,存在满足 \mod (n-1) = n-1 的数,那么答案就是 n-1。否则,答案就是拿 R 个糖果时最大,也就是 L \mod n + (R-L)。

T2 插入排序

代码

#include <bits/stdc++.h>
using namespace std;
int n, q;
int a[8001];
int main()
{
 ios::sync_with_stdio(false);
 cin.tie(0);
 cin >> n >> q;
 for (int i = 1; i <= n; ++i)
  cin >> a[i];
 for (int i = 1; i <= q; ++i)
 {
  int x;
  cin >> x;
  if (x == 1)
  {
   int x, y;
   cin >> x >> y;
   a[x] = y; // 直接修改
  }
  else if (x == 2)
  {
   int x;
   cin >> x;
   int id = n;
   // 寻找所有大于等于 a[x] 的数
   for (int i = 1; i < x; ++i)
    if (a[i] > a[x]) // 前面的等于的不会交换
     --id;
   for (int i = x + 1; i <= n; ++i)
    if (a[i] >= a[x]) // 后面部分等于的会交换
     --id;
   cout << id << '\n';
  }
 }
}

思路

纯暴力做法,时间复杂度 O(nq)。题目中 n 最大 8000,q 最大 2\times{10}^5,相乘后 8 \times {10}^8,卡常后能过。

T3 网络连接

代码

#include <bits/stdc++.h>
using namespace std;
/// @brief 地址结构体
struct comp
{
 unsigned char a, b, c, d;
 unsigned short e;
 bool operator==(comp com)
 {
  return a == com.a && b == com.b && c == com.c && d == com.d && e == com.e;
 }
} a[1001];
int n, len;
/// @brief 正则表达式
const regex reg(R"raw((0|(?:[1-9][0-9]{0,2}))\.(0|(?:[1-9][0-9]{0,2}))\.(0|(?:[1-9][0-9]{0,2}))\.(0|(?:[1-9][0-9]{0,2})):(0|(?:[1-9][0-9]{0,4})))raw");
// ---------------(匹配一个1-3位无前导零的整数)----------------------------------------------------------------------------(匹配一个0-5位无前导零的整数)
// (?:...): 表示不属于匹配组,仅用于辅助匹配

/// @brief 判断服务器地址是否合法,若合法输出 OK 并修改 com,若不合法输出 ERR,若冲突输出 FAIL
/// @param st 地址
/// @param com 存储的目标地址
void checks(string st, comp &com)
{
 std::smatch matches; // 匹配
 if (regex_match(st, matches, reg))
 {
  int a = atoi(matches[1].str().c_str());
  int b = atoi(matches[2].str().c_str());
  int c = atoi(matches[3].str().c_str());
  int d = atoi(matches[4].str().c_str());
  int e = atoi(matches[5].str().c_str());
  // 判断范围
  if (a < 0 || a > 255)
  {
   cout << "ERR\n";
   return;
  }
  if (b < 0 || b > 255)
  {
   cout << "ERR\n";
   return;
  }
  if (c < 0 || c > 255)
  {
   cout << "ERR\n";
   return;
  }
  if (d < 0 || d > 255)
  {
   cout << "ERR\n";
   return;
  }
  if (e < 0 || e > 65535)
  {
   cout << "ERR\n";
   return;
  }
  com.a = a;
  com.b = b;
  com.c = c;
  com.d = d;
  com.e = e;
  // 判断是否重复
  for (int i = 1; i < len; ++i)
   if (com == ::a[i])
   {
    cout << "FAIL\n";
    return;
   }
  cout << "OK\n";
 }
 else // 匹配失败
  cout << "ERR\n";
}
/// @brief 判断客户端的服务器地址是否合法,若合法输出 OK,若不合法输出 ERR,若服务器不存在输出 FAIL
/// @param st 服务器地址
void checkc(string st)
{
 comp com; // 临时存储地址
 std::smatch matches;
 if (regex_match(st, matches, reg))
 {
  int a = atoi(matches[1].str().c_str());
  int b = atoi(matches[2].str().c_str());
  int c = atoi(matches[3].str().c_str());
  int d = atoi(matches[4].str().c_str());
  int e = atoi(matches[5].str().c_str());
  if (a < 0 || a > 255)
  {
   cout << "ERR\n";
   return;
  }
  if (b < 0 || b > 255)
  {
   cout << "ERR\n";
   return;
  }
  if (c < 0 || c > 255)
  {
   cout << "ERR\n";
   return;
  }
  if (d < 0 || d > 255)
  {
   cout << "ERR\n";
   return;
  }
  if (e < 0 || e > 65535)
  {
   cout << "ERR\n";
   return;
  }
  com.a = a;
  com.b = b;
  com.c = c;
  com.d = d;
  com.e = e;
 }
 else
 {
  cout << "ERR\n";
  return;
 }
 for (int i = 1; i <= len; ++i)
  if (com == a[i])
  {
   cout << i << '\n';
   return;
  }
 cout << "FAIL\n";
}
int main()
{
 ios::sync_with_stdio(false);
 cin.tie(0);
 cin >> n;
 for (int i = 1; i <= n; ++i)
 {
  string st;
  cin >> st;
  if (st == "Server") // 服务器
  {
   cin >> st;
   checks(st, a[++len]);
  }
  else if (st == "Client") // 客户端
  {
   cin >> st;
   ++len;
   checkc(st);
  }
  // cout << "a[" << wz << "]: " << '|' << (int)a[wz].a << '.' << (int)a[wz].b << '.' << (int)a[wz].c << '.' << (int)a[wz].d << ':' << (int)a[wz].e << '|' << '\n';
 }
}

思路

模拟题。使用正则表达式匹配,然后判断是否合法,然后判断是否重复。

T4 小熊的果篮

代码

#include <bits/stdc++.h>
using namespace std;
int n;
/// @brief 水果
struct fr
{
    /// @brief 水果类型(true:苹果,false:橘子)
    bool fru;
    /// @brief 可以装(没有用过)
    bool can;
    /// @brief 下标
    int xb;
};
/// @brief 已经选过的水果
int num;
vector<fr> f;
int main()
{
    cin >> n;
    f.push_back(fr{(bool)0, false, 0});
    for (int i = 1; i <= n; ++i)
    {
        int x;
        cin >> x;
        f.push_back(fr{(bool)x, true, i});
    }
    while (num < n)
    {
        for (int wz = 1; wz <= n;)
        {
            // 寻找下一个可以用的水果
            while (wz <= n && !f[wz].can)
                ++wz;
            bool x = f[wz].fru;
            // 标记为已经使用
            f[wz].can = false;
            // 输出下标
            cout << f[wz].xb << ' ';
            // 下一个可以用的
            while (wz <= n && !f[wz].can)
                ++wz;
            // 寻找所有相同类型的水果
            for (; f[wz].fru == x;)
            {
                // 下一个
                ++wz;
                while (wz <= n && !f[wz].can)
                    ++wz;
            }
            // 已经选过的水果加一
            ++num;
        }
        cout << '\n';
    }
}

思路

暴力枚举,时间复杂度 O(n^2),用结构体记录每个水果的信息,枚举每个水果块,输出第一个并标记为已经使用。