#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string.h>
#include <map>
#include <utility>
#include <algorithm>
#include <stack>
using namespace std;
int all[1000005];
int temp[1000005];
int ans[1000005];
bool have[5][100005]={0};
int cha[100005];
int DP[5][100005]={0};
vector < pair < int , int > > how;
int N,M;
int F(int x,int y)
{
int t=0,i;
if(have[x][y]) return DP[x][y];
if(y==0) return 0;
have[x][y]=1;
//printf("%d %d %d %d\n",x,y,how[y].first,how[y].second);
if(x==0)
{
if((how[y].second-how[y].first+1)%2==0) DP[x][y]=F(0,y-1);
else DP[x][y]=F(1,y-1)+min(cha[how[y].first],cha[how[y-1].second]);
}
else
{
if((how[y].second-how[y].first)%2==0) DP[x][y]=F(0,y-1);
else DP[x][y]=F(1,y-1)+min(cha[how[y].first],cha[how[y-1].second]);
}
for(i=0;i<=how[y].second;i++) if(temp[i]!=temp[how[y].second]) t+=cha[i];
DP[x][y]=min(DP[x][y],t);
//printf("%d %d %d %d\n",x,y,DP[x][y],t);
return DP[x][y];
}
int main()
{
int i,j,k,con=0,K,now,l,t=0;
scanf("%d %d",&N,&M);
for(i=0;i<N;i++) scanf("%d",&all[i]);
for(i=1;i<=M;i++) ans[i]=1e9;
for(i=1;i<=M;i++)
{
for(j=1;j<=M;j++)
{
con=0;
for(k=0;k<N;k++)
{
if(all[k]!=i&&all[k]!=j)
{
con++;
temp[k]=i;
cha[k]=0;
}
else
{
cha[k]=1;
temp[k]=all[k];
}
}
how.clear();
now=0;
l=0;
for(k=0;k<N;k++)
{
if(k&&temp[k]!=temp[k-1])
{
how.push_back(make_pair(l,k-1));
l=k;
}
}
how.push_back(make_pair(l,k-1));
K=how.size();
for(k=0;k<=K;k++) have[0][k]=have[1][k]=0;
ans[i]=min(ans[i],min(F(0,K-1),F(1,K-1))+con);
ans[j]=min(ans[j],min(F(0,K-1),F(1,K-1))+con);
for(k=0;k<N;k++) temp[k]=all[N-k-1];
con=0;
for(k=0;k<N;k++)
{
if(temp[k]!=i&&temp[k]!=j)
{
con++;
temp[k]=i;
cha[k]=0;
}
else cha[k]=1;
}
how.clear();
now=0;
l=0;
for(k=0;k<N;k++)
{
if(k&&temp[k]!=temp[k-1])
{
how.push_back(make_pair(l,k-1));
l=k;
}
}
//printf("\n");
how.push_back(make_pair(l,k-1));
K=how.size();
for(k=0;k<=K;k++) have[0][k]=have[1][k]=0;
//if(i==2&&j==4||i==4&&j==2) printf("ttttt\n"),F(1,K-1);
ans[i]=min(ans[i],min(F(0,K-1),F(1,K-1))+con);
ans[j]=min(ans[j],min(F(0,K-1),F(1,K-1))+con);
//if(i==2&&j==4||i==4&&j==2) printf("%d %d %d\n",F(0,K-1),F(1,K-1),con);
}
}
for(i=1;i<=M;i++) printf("%d\n",ans[i]);
return 0;
}
Compilation message
rope.cpp: In function 'int main()':
rope.cpp:45:23: warning: variable 'now' set but not used [-Wunused-but-set-variable]
45 | int i,j,k,con=0,K,now,l,t=0;
| ^~~
rope.cpp:45:29: warning: unused variable 't' [-Wunused-variable]
45 | int i,j,k,con=0,K,now,l,t=0;
| ^
rope.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
46 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
rope.cpp:47:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
47 | for(i=0;i<N;i++) scanf("%d",&all[i]);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
0 ms |
364 KB |
Output is correct |
22 |
Correct |
0 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
0 ms |
364 KB |
Output is correct |
22 |
Correct |
0 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Execution timed out |
2578 ms |
2792 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
0 ms |
364 KB |
Output is correct |
22 |
Correct |
0 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Execution timed out |
2578 ms |
2792 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
0 ms |
364 KB |
Output is correct |
22 |
Correct |
0 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Execution timed out |
2578 ms |
2792 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
0 ms |
364 KB |
Output is correct |
22 |
Correct |
0 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Execution timed out |
2578 ms |
2792 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |