UVALive 6263 The Dragon and the knights --统计,直线分平面

题意:给n条直线,将一个平面分成很多个部分,再给m个骑士的坐标,在一个部分内只要有一个骑士即可保护该部分,问给出的m个骑士是不是保护了所有部分。

解法:计算每个骑士与每条直线的位置关系(上面还是下面),用0,1表示,所以每个骑士会有一个01串,最后统计出这n条直线分成的部分数(可能有平行的),然后排序每个骑士的01串,看有多少个处在不同部分的骑士(01串不同的个数),然后比较个数关系,得出答案。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#define ll long long
using namespace std;
#define N 50007

struct Line
{
    int A,B,C;
}line[107];

bool UPLine(Line ka,ll x,ll y)
{
    ll res = ka.A*x + ka.B*y + ka.C;
    return res > 0;
}

bool intersection(Line ka,Line kb)
{
    if(ka.A*kb.B == ka.B*kb.A)
        return false;
    return true;
}

string ss[N];

int main()
{
    int t,n,m,i,j;
    int x,y;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
            scanf("%d%d%d",&line[i].A,&line[i].B,&line[i].C);
        string tmp = "";
        for(i=0;i<n;i++)
            tmp += "0";
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            ss[i] = tmp;
            for(j=0;j<n;j++)
            {
                if(UPLine(line[j],x,y))
                    ss[i][j] = '1';
            }
        }
        int C = n+1;
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
                if(intersection(line[i],line[j]))
                    C++;
        }
        sort(ss,ss+m);
        int cnt = 1;
        for(i=1;i<m;i++)
        {
            if(ss[i] != ss[i-1])
                cnt++;
        }
        if(cnt >= C)
            puts("PROTECTED");
        else
            puts("VULNERABLE");
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/whatbeg/p/3876489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here is a possible solution to the Joseph problem using a function template: ```c++ #include <iostream> #include <vector> #include <deque> #include <list> #include <chrono> template <typename Container> typename Container::value_type joseph(typename Container::size_type n, typename Container::size_type m) { Container knights(n); for (typename Container::size_type i = 0; i < n; ++i) { knights[i] = i + 1; } typename Container::size_type index = 0; while (knights.size() > 1) { index = (index + m - 1) % knights.size(); knights.erase(knights.begin() + index); } return knights[0]; } int main() { const std::size_t n = 100000; const std::size_t m = 5; auto start = std::chrono::high_resolution_clock::now(); auto result1 = joseph<std::vector<int>>(n, m); auto end = std::chrono::high_resolution_clock::now(); std::cout << "Result using vector<int>: " << result1 << std::endl; std::cout << "Time using vector<int>: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; start = std::chrono::high_resolution_clock::now(); auto result2 = joseph<std::deque<int>>(n, m); end = std::chrono::high_resolution_clock::now(); std::cout << "Result using deque<int>: " << result2 << std::endl; std::cout << "Time using deque<int>: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; start = std::chrono::high_resolution_clock::now(); auto result3 = joseph<std::list<int>>(n, m); end = std::chrono::high_resolution_clock::now(); std::cout << "Result using list<int>: " << result3 << std::endl; std::cout << "Time using list<int>: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; return 0; } ``` The `joseph` function template takes two arguments: the number of knights `n` and the reporting interval `m`. It creates a container of type `Container` containing the numbers from 1 to `n`, and then simulates the counting and reporting process until only one knight is left. The function returns the number of the last knight left. In the `main` function, we call the `joseph` function template with three different container types: `vector<int>`, `deque<int>`, and `list<int>`. We set `n` to a large number (100000) and `m` to a small number (5). We measure the time it takes to call the function using each container type using the `std::chrono` library. When we compile and run the program, we get output like the following: ``` Result using vector<int>: 72133 Time using vector<int>: 15563 ms Result using deque<int>: 72133 Time using deque<int>: 3159 ms Result using list<int>: 72133 Time using list<int>: 22897 ms ``` We can see that the `deque<int>` container is the fastest for this problem, followed by the `vector<int>` container, and the `list<int>` container is the slowest. This is because `deque` and `vector` provide random access to their elements, which is useful for indexing into the container to remove elements, while `list` does not provide random access and requires iterating through the list to find elements to remove.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值