Submission #49672663
Source Code Expand
#include <bits/stdc++.h>
#include <atcoder/maxflow>
using namespace std;
#define rep(i,a,n) for (int i=a;i<(int)(n);i++)
int read(){int r;scanf("%d",&r);return r;}
int n,m;
const int INF=100*100000;
char s[100][100010];
int ans = INF;
vector<pair<int,vector<int> > > right_[10]; // {tick, vector<int> left}
int solve(const vector<pair<int,vector<int> > >&right){
int l = 0;
int r = INF+1;
auto check=[&](int t){
int S = 0;
int T = 1;
int loff = 2;
int roff = 2+n;
auto g = atcoder::mf_graph<int>(2+n+right.size());
rep(i,0,n) g.add_edge(S,loff+i,1); // S -> string
rep(j,0,size(right)) {
auto &[tick,lefts] = right[j];
g.add_edge(roff+j,T,t/m+int((t%m) >= tick));
for(auto left:lefts) g.add_edge(loff+left, roff+j, n);
}
return g.flow(S,T,n) == n;
};
while(r-l > 1){
int mid = (l+r)/2;
if(check(mid)){
r = mid;
}else{
l = mid;
}
}
return check(l)?l:r;
}
int main(){
n=read(); // <= 100
m=read();
rep(i,0,n) scanf("%s",s[i]);
vector count(10,vector<int>(n,0)); // count[char][row]
rep(i,0,n) rep(j,0,m) {
auto res = ++count[s[i][j]-'0'][i];
if(res > n+1) s[i][j] = '9'+1; // 无效化, 让左侧最多配前n个 !!!
}
rep(j,0,m) {
vector pos(11,vector<int>()); // 多一维无效化 !!!
rep(i,0,n) pos[s[i][j]-'0'].push_back(i);
rep(c,0,10) if(pos[c].size()) right_[c].push_back({j,pos[c]});
}
rep(v,0,9+1) {
int sum = 0;
rep(i,0,n) sum += bool(count[v][i]);
if(sum == n) ans = min(ans, solve(right_[v]));
}
printf("%d\n",ans==INF?-1:ans);
return 0;
}
Submission Info
Submission Time
2024-01-26 22:42:52+0900
Task
G - Slot Strategy 2 (Hard)
User
cromarmot
Language
C++ 20 (gcc 12.2)
Score
600
Code Size
1615 Byte
Status
AC
Exec Time
170 ms
Memory
15088 KiB
Compile Error
Main.cpp: In function ‘int read()’:
Main.cpp:5:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
5 | int read(){int r;scanf("%d",&r);return r;}
| ~~~~~^~~~~~~~~
Main.cpp: In function ‘int main()’:
Main.cpp:43:19: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
43 | rep(i,0,n) scanf("%s",s[i]);
| ~~~~~^~~~~~~~~~~
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
600 / 600
Status
Set Name
Test Cases
Sample
sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All
random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
Case Name
Status
Exec Time
Memory
random_01.txt
AC
170 ms
14912 KiB
random_02.txt
AC
22 ms
5048 KiB
random_03.txt
AC
128 ms
9868 KiB
random_04.txt
AC
13 ms
4272 KiB
random_05.txt
AC
166 ms
15088 KiB
random_06.txt
AC
98 ms
10884 KiB
random_07.txt
AC
116 ms
9188 KiB
random_08.txt
AC
64 ms
8436 KiB
random_09.txt
AC
96 ms
13900 KiB
random_10.txt
AC
6 ms
3788 KiB
random_11.txt
AC
1 ms
4264 KiB
random_12.txt
AC
1 ms
3784 KiB
random_13.txt
AC
92 ms
13520 KiB
random_14.txt
AC
91 ms
13744 KiB
random_15.txt
AC
92 ms
13644 KiB
random_16.txt
AC
90 ms
13736 KiB
random_17.txt
AC
82 ms
14008 KiB
random_18.txt
AC
85 ms
14188 KiB
random_19.txt
AC
87 ms
13912 KiB
random_20.txt
AC
86 ms
14100 KiB
random_21.txt
AC
95 ms
13804 KiB
random_22.txt
AC
97 ms
13616 KiB
random_23.txt
AC
1 ms
3780 KiB
sample_01.txt
AC
1 ms
3928 KiB
sample_02.txt
AC
1 ms
3900 KiB
sample_03.txt
AC
1 ms
3876 KiB
sample_04.txt
AC
1 ms
3736 KiB