[Codeforces-Gym](101606A)Alien Sunset ---- 暴力枚举

版权声明:本文为博主原创文章,转载请预先通知博主(〃'▽'〃)。 https://blog.csdn.net/m0_37624640/article/details/82153810

题目链接

做法:

  • 这道题目难的不是算法,而是题意的理解。
  • 题意是说,银行中有很多种个居住点,每个居住点的一天的周期是不同的,而且日升和日落的时间也是不同的。
  • 有一个叫“居住点时钟”的钟,从0开始计时,问经过多小时后,这些个居住点都处在黑夜中。如果经过的时间超过居住点中最大周期的1825倍,还无法找到某一时刻,那么输出impossible。
  • 因为n 最大才为 20,最大的居住点周期也是100小时/day
  • 所以我们可以枚举。
  • 通过题意,我们可以把居住点时钟看做   max(周期)*1825 小时制 (就是我们地球的24小时制的意思才不多)
  • 然后枚举[0,max(周期)*1825]这些时刻,通过mod每个居住点的周期,就可以知道当前时刻在这一居住点的时间,然后hash记录一下。最终确保n个居住点都在同一时刻处在黑暗中即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 2e5+5;
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
struct settle
{
    int s;
    int e;
    int h;
};
settle a[105];
int ans[maxm];
bool judge(int h,int x)
{
    if(a[x].s>a[x].e)
    {
        if(h>=a[x].e && h<=a[x].s) return true;
        ///else return false;
    }
    else
    {
        if(h>=a[x].e || h<=a[x].s) return true;
        ///else return false;
    }
    return false;
}
int main()
{
    #ifdef LOCAL_FILE
    freopen("in.txt","r",stdin);
    #endif // LOCAL_FILE
    ios_base::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int n,mx = -1;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i].h>>a[i].s>>a[i].e;
        mx = max(mx,a[i].h);
    }
    for(int i=0;i<=mx*1825;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(judge(i%a[j].h,j)) ans[i]++;
        }
    }
    int res = -1;
//    for(int i=0;i<=mx;i++)
//    {
//        cout<<ans[i]<<endl;
//    }
    for(int i=0;i<=mx*1825;i++)
    {
        if(ans[i] == n)
        {
            res = i;
            break;
        }
    }
    if(res!=-1) cout<<res<<endl;
    else cout<<"impossible"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37624640/article/details/82153810