제출 #328258

#제출 시각아이디문제언어결과실행 시간메모리
328258daniel920712Rope (JOI17_rope)C++14
45 / 100
2554 ms5092 KiB
#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};
int Con[5][100005]={0};
int tt[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;

    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],Con[tt[temp[how[y].second]]][how[y].second]);

    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;
            tt[i]=0;
            tt[j]=1;
            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;
            Con[0][0]=0;
            Con[1][0]=0;
            for(k=0;k<N;k++)
            {
                if(temp[k]==j&&cha[k]) Con[0][k]++;
                if(temp[k]==i&&cha[k]) Con[1][k]++;
                Con[0][k+1]=Con[0][k];
                Con[1][k+1]=Con[1][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;
            Con[0][0]=0;
            Con[1][0]=0;
            for(k=0;k<N;k++)
            {
                if(temp[k]==j&&cha[k]) Con[0][k]++;
                if(temp[k]==i&&cha[k]) Con[1][k]++;
                Con[0][k+1]=Con[0][k];
                Con[1][k+1]=Con[1][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(i=1;i<=M;i++) printf("%d\n",ans[i]);
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

rope.cpp: In function 'int F(int, int)':
rope.cpp:24:9: warning: unused variable 't' [-Wunused-variable]
   24 |     int t=0,i;
      |         ^
rope.cpp:24:13: warning: unused variable 'i' [-Wunused-variable]
   24 |     int t=0,i;
      |             ^
rope.cpp: In function 'int main()':
rope.cpp:47:23: warning: variable 'now' set but not used [-Wunused-but-set-variable]
   47 |     int i,j,k,con=0,K,now,l,t=0;
      |                       ^~~
rope.cpp:47:29: warning: unused variable 't' [-Wunused-variable]
   47 |     int i,j,k,con=0,K,now,l,t=0;
      |                             ^
rope.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   48 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
rope.cpp:49:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   49 |     for(i=0;i<N;i++) scanf("%d",&all[i]);
      |                      ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...