QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#657883#7624. Mystery of PrimewxiaobaivvWA 4ms11356kbC++142.3kb2024-10-19 15:38:492024-10-19 15:38:49

Judging History

你现在查看的是最新测评结果

  • [2024-10-19 15:38:49]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:11356kb
  • [2024-10-19 15:38:49]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<unordered_set>
using namespace std;
const int N=3e5;
unordered_set<int> bp;
vector<int> prime;
bool bl[N];
void init()
{
    bl[1]=1;
    for(int i=2;i<N;i++)
    {
        if(!bl[i]) {prime.push_back(i);bp.insert(i);}
        for(auto c:prime)
        {
            if(i*c>=N) break;
            bl[i*c]=1;
            if(i%c==0) break;
        }
    }
}
int dp[N][2][2],ol[N]; //0 不变
int arr[N];

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    init();
    memset(dp,0x3f,sizeof dp);
    memset(ol,0x3f,sizeof ol);

    int n; cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
        int fg=arr[i]%2;
        if(i==1) 
        {
            dp[i][1][0]=dp[i][1][1]=1;
            dp[i][0][fg]=0;

            if(arr[i]==1) ol[i]=0;
            else ol[i]=1;
            continue;
        }

        if(bp.count(arr[i]+arr[i-1])) dp[i][0][fg]=min(dp[i-1][0][fg^1],dp[i-1][1][fg^1]);
        else dp[i][0][fg]=dp[i-1][1][fg^1];

        dp[i][0][fg^1]=min(dp[i-1][0][fg],dp[i-1][1][fg])+1;

        if(arr[i]==1) dp[i][0][1]=min(dp[i][0][1],ol[i-1]);
        if(bp.count(arr[i]+1)) dp[i][0][fg]=min(dp[i][0][fg],ol[i-1]);
        

        dp[i][1][fg]=min(dp[i-1][0][fg^1],dp[i-1][1][fg^1])+1;
        dp[i][1][fg^1]=min(dp[i-1][0][fg],dp[i-1][1][fg])+1;

        dp[i][1][0]=min(dp[i][1][0],ol[i-1]+1);

        if(arr[i]==1) 
        {
            int tf=1;
            if(bp.count(arr[i-1]+1)) tf=0;
            ol[i]=min(dp[i-1][1][0],ol[i-1]);
            ol[i]=min(ol[i],ol[i-1]);
            ol[i]=min(ol[i],dp[i-1][0][arr[i-1]%2]+tf);
        }
        else 
        {
            
            int tf=1;
            if(bp.count(arr[i-1]+1)) tf=0;

            ol[i]=min(dp[i-1][1][0],ol[i-1])+1;
            ol[i]=min(ol[i],ol[i-1]+1);
            ol[i]=min(ol[i],dp[i-1][0][arr[i-1]%2]+1+tf);
            // if(i==10)
            // {
            //     cout<<ol[i]<<":::: ";
            //   cout<<dp[i-1][0][arr[i-1]%2]<<"!!\n";
            // }
        }
    
        
        // cout<<i<<" "<<dp[i][0][arr[i]%2]<<'\n';
    }
    int mi=min(dp[n][1][arr[n]%2],dp[n][0][arr[n]%2]);
    mi=min(mi,min(dp[n][1][(arr[n]%2)^1],dp[n][0][(arr[n]%2)^1]));
    mi=min(mi,ol[n]);
    cout<<mi;
 
    
    
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 11356kb

input:

6
1 5 1 4 4 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 4ms
memory: 10640kb

input:

9
30 6 7 12 15 8 20 17 14

output:

4

result:

ok 1 number(s): "4"

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 10768kb

input:

1568
119 519 706 1003 1317 322 25 1466 816 525 1 1122 38 1511 774 515 274 780 647 230 1602 1051 810 1 1 1232 1 1202 1583 412 1111 168 309 1181 184 1260 491 764 809 1213 804 1470 1 1087 1235 1004 673 1338 1333 1392 1036 1539 268 1 712 727 297 404 1317 36 463 1067 1133 693 931 46 1 100 1608 965 1 1406...

output:

724

result:

wrong answer 1st numbers differ - expected: '733', found: '724'