Submission #36424520


Source Code Expand

#include <bits/stdc++.h>
#define st first
#define nd second
#define db double
#define re register
#define pb push_back
#define mk make_pair
#define int long long
#define ldb long double
#define pii pair<int, int>
#define ull unsigned long long
#define mst(a, b) memset(a, b, sizeof(a))
using namespace std;
const int N = 2e5 + 10, INF = 1e18;
inline int read()
{
  int s = 0, w = 1;
  char ch = getchar();
  while(ch < '0' || ch > '9') { if(ch == '-') w *= -1; ch = getchar(); }
  while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
int n, m, ans;
int a[N], s[N];
bool vis[N];
inline int nex(int x) { return (x + 1) % n; }
inline int lst(int x) { return (x - 1 + n) % n; }
inline bool chk(int x, int y) { return (x == y) || (((x + 1) % m) == y); }
signed main()
{
  n = read(), m = read(), ans = INF;
  for(re int i = 0; i < n; i++) a[i] = read();
  sort(a, a + n), s[0] = a[0];
  for(re int i = 1; i < n; i++) s[i] = s[i - 1] + a[i];
  bool flag = true;
  for(re int i = 0; i < n; i++)
    if(!chk(a[i], a[nex(i)])) flag = false;
  if(flag) puts("0"), exit(0); //成环
  for(re int i = 0; i < n; i++){
    if(!vis[i]){
      int l = i, r = i; vis[i] = true;
      while(chk(a[lst(l)], a[l])) l = lst(l), vis[l] = true;
      while(chk(a[r], a[nex(r)])) r = nex(r), vis[r] = true;
      if(l > i){
        int res = s[n - 1] - s[l - 1] + s[r];
        ans = min(ans, s[n - 1] - res);
      }
      else ans = min(ans, s[n - 1] - (s[r] - (!l ? 0 : s[l - 1])));
    }
  }
  printf("%lld\n", ans);
  return 0;
}
/*
1
10
9 9 4 7 3 10 10 8 4 3
*/

Submission Info

Submission Time
Task D - Takahashi's Solitaire
User Booksnow
Language C++ (GCC 9.2.1)
Score 400
Code Size 1634 Byte
Status AC
Exec Time 41 ms
Memory 7008 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:32:14: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
   32 |   for(re int i = 0; i < n; i++) a[i] = read();
      |              ^
./Main.cpp:34:14: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
   34 |   for(re int i = 1; i < n; i++) s[i] = s[i - 1] + a[i];
      |              ^
./Main.cpp:36:14: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
   36 |   for(re int i = 0; i < n; i++)
      |              ^
./Main.cpp:39:14: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
   39 |   for(re int i = 0; i < n; i++){
      |              ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 40
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, example0.txt, example1.txt, example2.txt
Case Name Status Exec Time Memory
000.txt AC 8 ms 3416 KiB
001.txt AC 13 ms 6472 KiB
002.txt AC 21 ms 6600 KiB
003.txt AC 28 ms 6636 KiB
004.txt AC 33 ms 6900 KiB
005.txt AC 34 ms 6856 KiB
006.txt AC 34 ms 6808 KiB
007.txt AC 20 ms 7004 KiB
008.txt AC 11 ms 6852 KiB
009.txt AC 21 ms 6896 KiB
010.txt AC 4 ms 3752 KiB
011.txt AC 20 ms 5204 KiB
012.txt AC 33 ms 6592 KiB
013.txt AC 37 ms 6900 KiB
014.txt AC 41 ms 6856 KiB
015.txt AC 38 ms 6912 KiB
016.txt AC 37 ms 6940 KiB
017.txt AC 38 ms 6900 KiB
018.txt AC 39 ms 6856 KiB
019.txt AC 14 ms 6640 KiB
020.txt AC 23 ms 6648 KiB
021.txt AC 20 ms 6480 KiB
022.txt AC 22 ms 6592 KiB
023.txt AC 33 ms 6916 KiB
024.txt AC 30 ms 6856 KiB
025.txt AC 33 ms 6856 KiB
026.txt AC 34 ms 6784 KiB
027.txt AC 36 ms 6780 KiB
028.txt AC 35 ms 6804 KiB
029.txt AC 35 ms 6856 KiB
030.txt AC 34 ms 6956 KiB
031.txt AC 37 ms 7008 KiB
032.txt AC 37 ms 7008 KiB
033.txt AC 35 ms 6900 KiB
034.txt AC 35 ms 6900 KiB
035.txt AC 32 ms 6956 KiB
036.txt AC 37 ms 7008 KiB
example0.txt AC 2 ms 3604 KiB
example1.txt AC 2 ms 3520 KiB
example2.txt AC 2 ms 3588 KiB