Leaving the Bar -CF996E - 概率

博客给出题目链接https://codeforces.com/contest/996/problem/E,介绍该题解法很玄学,即对数组进行一种操作,若结果不满足条件,就对数组随机排列后再操作,重复此过程直至满足条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

附上原题链接:https://codeforces.com/contest/996/problem/E

这题的解法真的很玄学,简单来说就是对这个数组进行一种操作,如果该操作的结果不能满足条件,就对该数组进行随机排列,再进行这种操作,重复上述过程,知道满足条件。

 

#include<bits/stdc++.h>
#include<iostream>
#include<sstream>
#include<cmath>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-6
typedef long long ll;
const ll N = 1e5+10;
const ll L = 1.5e6 * 1.5e6;

struct node
{
    int x, y, id;
};


node a[N];
int ans[N];

int n;
int main()
{
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin >> a[i].x >> a[i].y;
        a[i].id = i;
    }

    while(1)
    {
        ll X = 0, Y = 0;
        for(int i=0; i<n; i++)
        {
            if( (X+a[i].x)*(X+a[i].x) + (Y+a[i].y)*(Y+a[i].y) <= (X-a[i].x)*(X-a[i].x) + (Y-a[i].y)*(Y-a[i].y) )
            {
                ans[ a[i].id ] = 1;
                X = X + a[i].x;
                Y = Y + a[i].y;
            }
            else
            {
                ans[a[i].id ] = -1;
                X = X - a[i].x;
                Y = Y - a[i].y;
            }
        }
        if(X*X + Y*Y <= L)
        {
            //cout << sqrt(X*X + Y*Y) <<endl;
            break;
        }    
        random_shuffle(a, a+n);

    }

    for(int i=0; i<n; i++)
    {
        cout << ans[i] << ' ';
    }

    //system("pause");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值