QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#463571#1361. Ant TypingMahmoudBassemWA 2ms3748kbC++141.4kb2024-07-05 01:07:112024-07-05 01:07:11

Judging History

This is the latest submission verdict.

  • [2024-07-05 01:07:11]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 3748kb
  • [2024-07-05 01:07:11]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

#define int int64_t
#define ld long double
#define el '\n'
#define fi first
#define se second

#define once_more_into_the_fray ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int INF = 1e17;
const int N = 1e5 + 10;
const int A = 9;
int dp[1 << A];
int n, m;
string s;
int adj[A][A];

bool relax(int &a, int b) {
  if (a <= b) return false;
  a = b;
  return true;
}

void run_case(int tc) {
  cin >> s;
  n = int(s.size());
  m = 9;
  s[0]--;
  for (int i = 0; i < n - 1; i++) {
    s[i + 1]--;
    adj[s[i] - '0'][s[i + 1] - '0'] += 1;
    adj[s[i + 1] - '0'][s[i] - '0'] += 1;
  }
  for (auto &i: dp)
    i = INF;
  dp[0] = 0;
  for (int mask = 0; mask < 1 << A; mask++) {
    for (int nxt = 0; nxt < m; nxt++) {
      if (1 << nxt & mask) continue;
      int cont = 0;
      int pos = __builtin_popcount(mask);
      for (int c = 0; c < m; c++) {
        if (1 << c & mask)
          cont += pos * adj[nxt][c];
        else if (c != nxt)
          cont -= pos * adj[nxt][c];
      }
      relax(dp[mask | 1 << nxt], dp[mask] + cont);
    }
  }

  cout << dp[(1 << m) - 1] + n << el;
}

int32_t main() {
  once_more_into_the_fray        /*Turn Off for Interactive Problems*/

  int _t = 1;
  //cin >> _t;
  for (int i = 1; i <= _t; i++) {
    run_case(i);
  }
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3556kb

input:

6

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3748kb

input:

352836512461179399826927828445163785261417666171453483576899676763764928341261962358737253818463814858565221466575498899898835568743316628247174676952381449147193191788177911797527361649543158616436321694172452689147288835568666918784929695569394655833978219612584637258492511247969998253315177569943...

output:

392933

result:

ok single line: '392933'

Test #3:

score: -100
Wrong Answer
time: 2ms
memory: 3692kb

input:

968884598119167786222882717561136271258178322114499631431168437152319338284128325566492371843153634373998119972663175955316621871196955773794711869424245474973253727335485699826672425921549421474494147229652553728521332495721653457163161741649486568718788497235884884271122261183582319332269114971817...

output:

394663

result:

wrong answer 1st lines differ - expected: '394664', found: '394663'