Codeforces_842

A.枚举一个区间,判断是否有数符合。

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

long long l,r,x,y,k;

int main()
{
    ios::sync_with_stdio(0);
    cin >> l >> r >> x >> y >> k;
    for(int i = x;i <= y;i++)
    {
        if(l <= i*k && i*k <= r)
        {
            cout << "YES" << endl;
            return 0;
        }
    }
    cout << "NO" << endl;
    return 0;
}
View Code

B.判断两个边界。

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

int r,d,n;

int main()
{
    ios::sync_with_stdio(0);
    cin >> r >> d >> n;
    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        double x,y,rr;
        cin >> x >> y >> rr;
        double dis = sqrt(x*x+y*y);
        if(dis-rr > r-d-1e-9 && dis+rr < r+1e-9)    ans++;
    }
    cout << ans << endl;
    return 0;
}
View Code

C.暴力判断每种gcd,set去重后实际的数量很少。

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

int n,a[200005],ans[200005] = {0};
vector<int> v[200005];
set<int> s[200005];

void dfs(int now,int pre)
{
    for(int t : s[pre]) s[now].insert(__gcd(t,a[now]));
    a[now] = __gcd(a[pre],a[now]);
    s[now].insert(a[pre]);
    for(int t : s[now]) ans[now] = max(ans[now],t);
    for(int t : v[now])
    {
        if(t == pre)    continue;
        dfs(t,now);
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1;i <= n;i++)   cin >> a[i];
    for(int i = 1;i < n;i++)
    {
        int x,y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    a[0] = 0;
    s[0].insert(0);
    dfs(1,0);
    for(int i = 1;i <= n;i++)   cout << ans[i] << " ";
    cout << endl;
    return 0;
}
View Code

D.前缀和,二分判断每一位,还要记录位置改变状态。

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

int n,m,cnt[5000005] = {0};

int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> m;
    for(int i = 1;i <= n;i++)
    {
        int x;
        cin >> x;
        cnt[x] = 1;
    }
    for(int i = 1;i <= 300000;i++)  cnt[i] += cnt[i-1];
    int t = 0;
    while(m--)
    {
        int z;
        cin >> z;
        t ^= z;
        int l = 0,r = (1<<19)-1;
        for(int i = 18;i >= 0;i--)
        {
            int tt = t&(1<<i),mid = (l+r)/2;
            if(tt)
            {
                if(cnt[r]-cnt[mid] < r-mid) l = mid+1;
                else    r = mid;
            }
            else
            {
                if(cnt[mid]-(l?cnt[l-1]:0) < mid-l+1)  r = mid;
                else    l = mid+1;
            }
        }
        cout << (l^t) << endl;
    }
    return 0;
}
View Code

 

posted @ 2017-08-30 03:37  zzzzzzzzhu  阅读(453)  评论(2编辑  收藏  举报