cordforce 495 补题 <未完>

题目链接:

http://codeforces.com/contest/1004/my

A. Sonya and Hotels

分类讨论

看第一个样例解释的时候没看到后面第二行还有一个19,想了半天为啥19不算

题意大致是选定一个位置,使得其到达原本那些位置的距离最小的为d

此处应注意,最小为d意味着不能距离小于d

所以可能的住所有三种情况,

1. 两个城市距离小于2d, 那么无法在其间选定

2. 两个城市距离等于2d, 可选定一个点

3. 两个城市距离大于2d, 可选定2个点

#include <iostream>
#include <algorithm>
#include <set>
#define MS 120
using namespace std;

int main(){
    int n, d, i;
    cin >> n >> d;
    int old[MS];
    for(i = 0; i < n; i++){
        cin >> old[i];
    }
    sort(old, old+n);
    int ans = 2;
    for(i = 1; i < n; i++){
        if(old[i]-old[i-1] == 2*d){
            ans++;
        }
        else if (old[i] - old[i-1] > 2*d){
            ans+=2;
        }
    }
    cout << ans << endl;     
}

B. Sonya and Exhibition

思维题

大致意思是在一个序列里摆放花朵,可以放玫瑰和百合,然后每个来看的人给出一个看的区间,将区间内的百合数乘以玫瑰数即该人获得的愉悦值

目标使愉悦值最大

开始时思考各种思路,想了怎么去遍历,但发现这些复杂度都过高了,后来查了一下大神解法,原来另有玄机

由于对于一个区间内,设玫瑰和百合数分别是a, b, 那么

a*b <= ((a+b)/2)2

而a+b为该区间长度,不变,因此为使得值最大,应该使a = b

所以区间内玫瑰和百合应该数目相同,可以想到交替放置的特解,对于偶数长度区间成立

对于奇数长度区间是最优解,因此成立

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
    }
    for(int i=1;i<=n;i++)
        if(i%2==0)
            cout<<0;
        else
            cout<<1;
    cout<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Phoenix-blog/p/9375323.html