QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#796272#7624. Mystery of PrimeMaxduan#WA 8ms22052kbC++231.8kb2024-12-01 15:37:082024-12-01 15:37:09

Judging History

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

  • [2024-12-01 15:37:09]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:22052kb
  • [2024-12-01 15:37:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define pii pair<int,int>
vector<int> prime;
#define maxn 1000005
int dp[maxn][2];//改成奇或偶
int dp1[maxn];//为1
int dp2[maxn];//不改
int ok[maxn];
int a[maxn];
int g[maxn];//奇偶

void sieve(int x){
    vector<int> arr(x+1);
    for(int i=2;i<=x;i++) {
        if(!arr[i]){
            prime.push_back(i);
            ok[i]=1;
        } 
        for(auto it:prime) {
            if(i*it>x)break;
            arr[i*it]=1;
            if(i%it==0)break;
        }
    }
}

void solve(){
    int n;cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        g[i]=(a[i]&1);
    }
    dp[1][0]=dp[1][1]=1;
    dp1[1]=(a[1]!=1);
    dp2[1]=0;
    for(int i=2;i<=n;i++){
        //改成奇 偶
        dp[i][1]=dp[i-1][0]+1;
        if(g[i-1]==0)dp[i][1]=min(dp[i][1],dp2[i-1]+1);
        dp[i][0]=dp[i-1][1]+1;
        if(g[i-1]==1)dp[i][0]=min(dp[i][0],dp2[i-1]+1);
        dp[i][0]=min(dp[i][0],dp1[i-1]+1);

        //这位为1
        dp1[i]=dp1[i-1];
        if(ok[a[i-1]+1])dp1[i]=min(dp1[i],dp2[i-1]);
        dp1[i]=min(dp1[i],dp[i][0]);
        
        dp1[i]+=(a[i]!=1);

        //不改
        dp2[i]=dp[i-1][g[i]^1];
        if(ok[a[i]+1])dp2[i]=min(dp2[i],dp1[i]);
        if(ok[a[i]+a[i-1]])dp2[i]=min(dp2[i],dp2[i-1]);
        //cout<<"i="<<i<<'\n';
        //cout<<"改成奇数 偶数="<<' '<<dp[i][0]<<' '<<dp[i][1]<<'\n';
        //cout<<"1="<<dp1[i]<<'\n';
        //cout<<"不改="<<dp2[i]<<'\n'<<'\n';
    }
    int ans=min({dp[n][0],dp[n][1],dp1[n],dp2[n]});
    cout<<ans<<'\n';
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    sieve(1e6);
    int T=1;//cin>>T;
    while(T--){
        solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 8ms
memory: 22052kb

input:

6
1 5 1 4 4 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 3ms
memory: 21784kb

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: 4ms
memory: 20260kb

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:

737

result:

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