【HT-021-Div.3】核桃CSP-J组周赛答案

以下为核桃编程官方提供的【HT-021-Div.3】核桃CSP-J组周赛答案,仅供参考

<center>
<h2>第一题</h2>
</center>

#include <bits/stdc++.h>
using namespace std;
int main()
{
  int l, r, ans = 0, maxx = 0;
  cin >> l >> r;
  for (int i = l; i <= r; i++)
  {
    int temp = i, last = 11, cnt = 0;
    while (temp)
    {
      int now = temp % 10;
      if (now < last)
      {
        cnt++;
        last = now;
      }
      else
      {
        break;
      }
      temp /= 10;
    }
    if (cnt >= maxx)
    {
      maxx = cnt;
      ans = i;
    }
  }
  cout << ans << endl;
}

<center>
<h2>第二题</h2>
</center>

#include <bits/stdc++.h>
using namespace std;
struct node
{
  int v[3], id;
};
node a[100005];
int ans[100005];
bool cmp1(node a, node b)
{
  return a.v[0] < b.v[0];
}
bool cmp2(node a, node b)
{
  return a.v[1] < b.v[1];
}
bool cmp3(node a, node b)
{
  return a.v[2] < b.v[2];
}
int main()
{
  memset(ans, 0, sizeof ans);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
  {
    cin >> a[i].v[0] >> a[i].v[1] >> a[i].v[2];
    a[i].id = i;
    sort(a[i].v, a[i].v + 3);
  }
  sort(a + 1, a + 1 + n, cmp1);
  int last = 0;
  for (int i = 1; i <= n; i++)
  {
    if (a[i].v[0] != a[i - 1].v[0])
    {
      last = i - 1;
    }
    ans[a[i].id] += last;
  }
  sort(a + 1, a + 1 + n, cmp2);
  last = 0;
  for (int i = 1; i <= n; i++)
  {
    if (a[i].v[1] != a[i - 1].v[1])
    {
      last = i - 1;
    }
    ans[a[i].id] += last;
  }
  sort(a + 1, a + 1 + n, cmp3);
  last = 0;
  for (int i = 1; i <= n; i++)
  {
    if (a[i].v[2] != a[i - 1].v[2])
    {
      last = i - 1;
    }
    ans[a[i].id] += last;
  }
  for (int i = 1; i <= n; i++)
  {
    cout << ans[i] << " ";
  }
}

<center>
<h2>第三题</h2>
</center>

#include <bits/stdc++.h>
using namespace std;
int main()
{
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  s = " " + s;
  vector<int> f(n + 2);
  for (int i = k + 1; i <= n; i++)
  {
    if (s[i] == s[i - k])
      f[i] = 1;
  }
  int l = 0, ans = 0;
  for (int i = k + 1; i <= n + 1; i++)
  {
    if (!f[i])
    {
      ans = max(ans, l);
      l = 0;
    }
    else
      l++;
  }
  cout << ans / k * k + k << endl;
}

<center>
<h2>第四题</h2>
</center>

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
long long n, m, num[maxn], dp[maxn];
int main()
{
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
  {
    cin >> num[i];
    num[i] += num[i - 1];
  }
  dp[0] = 1;
  for (int i = 1, j = 0; i <= n; i++)
  {
    if (i > 1)
    {
      dp[i] = dp[i - 1];
    }
    while (num[i] - num[j] >= m)
    {
      dp[i] = (dp[i] + dp[j]) % mod;
      j++;
    }
  }
  cout << dp[n];
}

1 条评论

  • 1