QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#728670#5983. Pretty Good Proportionwangchunjie27 ✓2251ms27756kbC++141.9kb2024-11-09 15:38:012024-11-09 15:38:03

Judging History

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

  • [2024-11-09 15:38:03]
  • 评测
  • 测评结果:27
  • 用时:2251ms
  • 内存:27756kb
  • [2024-11-09 15:38:01]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define int long long

const int N=5e5+5;
const long double eps=1e-12;

int T,n;
long double p;
char s[N];
int a[N];
int sum[N];
struct node
{
    int x;
    long double y;
    bool operator<(const node &T)const{
        if(fabs(y-T.y)<eps)
            return x<T.x;
        return y<T.y;
    }
}b[N];

void solve()
{
    cin>>n>>p;
    cin>>s+1;
    for(int i=1;i<=n;i++) a[i]=s[i]^48;
    for(int i=1;i<=n;i++)
        sum[i]=sum[i-1]+a[i];
    b[n+1]={0,0};
    for(int i=1;i<=n;i++)
        b[i]={i,p*i-sum[i]};
    sort(b+1,b+n+2);
    long double K1=1e14;
    int pos1=n+1;
    for(int i=1;i<=n;i++){
        long double k=(b[i+1].y-b[i].y)/(b[i+1].x-b[i].x);
        if(fabs(k-K1)<eps)
            pos1=min({pos1,b[i].x+1,b[i+1].x+1});
        else if(k<K1&&k>=0){
            K1=k;
            pos1=min(b[i].x+1,b[i+1].x+1);
        }
    }
    // cout<<pos1<<'\n';
    p=1-p;
    for(int i=1;i<=n;i++) a[i]^=1;
    for(int i=1;i<=n;i++)
        sum[i]=sum[i-1]+a[i];
    b[n+1]={0,0};
    for(int i=1;i<=n;i++)
        b[i]={i,p*i-sum[i]};
    sort(b+1,b+n+2);
    long double K2=1e14;
    int pos2=n+1;
    for(int i=1;i<=n;i++){
        long double k=(b[i+1].y-b[i].y)/(b[i+1].x-b[i].x);
        if(fabs(k-K2)<eps)
            pos2=min({pos2,b[i].x+1,b[i+1].x+1});
        else if(k<K2&&k>=0){
            K2=k;
            pos2=min(b[i].x+1,b[i+1].x+1);
        }
    }
    if(fabs(K1-K2)<eps)
        cout<<min(pos1,pos2)-1<<'\n';
    else if(K1<K2)
        cout<<pos1-1<<'\n';
    else
        cout<<pos2-1<<'\n';
}

signed main()
{
    // freopen("tii.in", "r", stdin);
    // freopen("tii.out", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>T;
	for(int i=1;i<=T;i++){
		cout<<"Case #"<<i<<": ";
		solve();
	}
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 3ms
memory: 7996kb

input:

100
10 0.827672
0010101011
4 0.932623
0100
1000 0.834002
011001110010111110000110101100010010100101101110110111100010101101111100110001011000110100010100011011000001100001010110111101111010110110000110011000111000011110101100100111111001111011011100111001011101010100111011100011110011100011110010001...

output:

Case #1: 6
Case #2: 1
Case #3: 10
Case #4: 0
Case #5: 0
Case #6: 1
Case #7: 0
Case #8: 0
Case #9: 0
Case #10: 0
Case #11: 0
Case #12: 4
Case #13: 5
Case #14: 564
Case #15: 0
Case #16: 0
Case #17: 0
Case #18: 0
Case #19: 0
Case #20: 0
Case #21: 0
Case #22: 0
Case #23: 0
Case #24: 844
Case #25: 0
Case...

result:

ok 100 lines

Subtask #2:

score: 22
Accepted

Test #2:

score: 22
Accepted
time: 2251ms
memory: 27756kb

input:

100
15 0.333333
000000000011000
10 0.418754
0101100001
2 0.499999
01
3 0.977951
001
2 0.249999
01
10 0.670229
0111011001
1000 0.500001
001101111110110010110000010010110001110010001101110111010011000010100011011101010110011011011010111110011100011000001000101011100011010100101101111110100101011010111...

output:

Case #1: 6
Case #2: 0
Case #3: 0
Case #4: 2
Case #5: 0
Case #6: 0
Case #7: 0
Case #8: 0
Case #9: 0
Case #10: 0
Case #11: 0
Case #12: 0
Case #13: 0
Case #14: 0
Case #15: 0
Case #16: 4333
Case #17: 0
Case #18: 0
Case #19: 123
Case #20: 0
Case #21: 0
Case #22: 0
Case #23: 0
Case #24: 0
Case #25: 0
Case...

result:

ok 100 lines

Extra Test:

score: 0
Extra Test Passed