2018分组赛Round2 (To be continue)

A CodeForces-618C

题解

按照点坐标排序,选取两个点,枚举另一个点就可以。需要判断是否3点共线,用到向量的叉积。

代码

#include<bits/stdc++.h>
using namespace std;
const int nmax = 1e5 + 100;
const int INF = 0x3f3f3f3f;
typedef long long ll;
typedef double db;
typedef struct{
    ll x,y;
    int id;
}point;
point p[nmax];
int n;
inline ll dis(point & a, point & b){
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
inline bool cmp(point & a, point & b){
    if(a.x != b.x) return a.x < b.x;
    else return a.y < b.y;
}
inline bool judge(point & a, point & b, point & c){
//    double len1 = sqrt(dis(a,b)) ,len2 = sqrt(dis(a,c)) , len3 = sqrt(dis(b,c));
//    if(len1 + len2 > len3 && len1 + len3 > len2 && len2 + len3 > len1) return true;
//    else return false;
    // must not in a line
    ll ax = c.x - a.x, ay = c.y - a.y;
    ll bx = b.x - a.x, by = b.y - a.y;
    if(ax * by - ay * bx == 0) return false;
    else return true;
}
int main() {
    scanf("%d",&n);
    for(int i = 1;i<=n;++i){
        scanf("%I64d %I64d",&p[i].x, &p[i].y);
        p[i].id = i;
    }
    sort(p+1,p+n+1,cmp);
    for(int i = 3;i<=n;++i){
        if(judge(p[1],p[2],p[i])){
            printf("%d %d %d\n",p[1].id,p[2].id,p[i].id);
            break;
        }
    }
    return 0;
}

B CodeForces - 892C

题解

如果序列中有1,那么答案就是序列长度减1的个数。
如果 g c d ( a 1 , a 2 , a 3 , . . . a n ) ! = 1 ,那么无解。
否则的话,需要先构造出来一个1,并且需要知道构造出1的最小次数。
考虑序列a,我们相邻的两个元素求gcd,如果有1,那么意味着可以直接一次求出来,次数就1,否则,我们对求完gcd的序列,继续两两相邻元素求gcd,知道出现1为止,次数就是出现1的次数。假设这个次数为t,那么答案就是 n + t 1

代码

#include<bits/stdc++.h>
using namespace std;
const int nmax = 1e6 ;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n,m;
int a[2005];
int nowgcd = 0;
int numofone = 0;
int main() {
    scanf("%d",&n);
    for(int i = 1;i<=n;++i){
        scanf("%d",&a[i]);
        if(i == 1) nowgcd = a[i];
        else nowgcd = __gcd(nowgcd,a[i]);
        if(a[i] == 1) numofone ++;
    }
    if(nowgcd != 1) printf("-1\n");
    else{
        if(numofone) printf("%d\n",n-numofone);
        else{
            bool isfind = false;
            int t = 0;
            for(int i = 1;i<=n;++i){
                for(int j = 1;j<=n-i;++j){
                    a[j] = __gcd(a[j],a[j+1]);
                    if(a[j] == 1){
                        t = i;
                        isfind = true;
                        break;
                    }
                }
                if(isfind) break;
            }
            printf("%d\n",t + n - 1);
        }
    }
    return 0;
}

C CodeForces - 425A

题解

首先看到题目的数据范围很小,就应该能想到暴力枚举的方法。
我们暴力枚举区间的左右端点 l , r ,然后找出在这个区间内的数和不在这个区间内的数,将这个区间中的前k小替换成区间外的前k大,要考虑替换的时候是否满足更优,否则不换。
对替换后的结果求和,并更新答案即可。

代码

#include<bits/stdc++.h>
using namespace std;
const int nmax = 250 ;
const int INF = 0x3f3f3f3f;
int n,k;
int a[nmax];
vector<int> inval,outval;
int main() {
    scanf("%d %d",&n,&k);
    int ans = -INF;
    for(int i = 1;i<=n;++i) scanf("%d",&a[i]);
    for(int l = 1;l<=n;++l){
        for(int r = l;r<=n;++r){
            for(int i = 1;i<=n;++i){
                if(i >= l && i <= r) inval.push_back(a[i]);
                else outval.push_back(a[i]);
            }
            sort(inval.begin(),inval.end(),less<int>());
            sort(outval.begin(),outval.end(),greater<int>());
//            int t = min(k,min(inval.size(),outval.size()));
            int pos = 0,times = 0;
            if(outval.size() == 0 || inval[0] >= outval[0]){
                // do nothing
            }else{
                for(int i = 0;i<inval.size();++i){
                    if(inval[i]<outval[pos]){
                        swap(inval[i],outval[pos]);
                        pos++;
                        times++;
                    }
                    if(pos == outval.size() || times == k) break;
                }
            }
            int temp = 0;
            for(int i = 0;i<inval.size();++i) temp+=inval[i];
            ans = max(ans,temp);
            inval.clear(); outval.clear();
        }
    }
    printf("%d\n",ans);
    return 0;
}

D HYSBZ 1188

题解

博弈题目,不会,待补。

E HYSBZ 1877

题解

最小费用最大流,不会,待补。

F HYSBZ 4710

题解

组合数学。
首先需要知道挡板法,在此基础上,计算每一个的,然后再用容斥就行了。
注意数组的范围。

代码

#include<bits/stdc++.h>
using namespace std;
const int nmax = 2e3+100 ;
const int INF = 0x3f3f3f3f;
const long long MOD = 1e9+7;
typedef long long ll;
int n,m;
ll a[nmax];
ll C[nmax][nmax];
ll ans = 0;
void init(){
//    C[0][0] = 1;
    for(int i = 0;i<nmax;++i){
        C[i][0] = 1;
        for(int j = 1;j<=i;++j){
            C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD;
        }
    }
}
int main() {
    init();
    scanf("%d %d",&n,&m);
    for(int i = 1;i<=m;++i) scanf("%lld",&a[i]);
    for(int i = 0;i<n;++i){
        ll temp = C[n][i];
        for(int j = 1;j<=m;++j)
            temp = (temp * C[n + a[j] - i - 1][a[j]]) % MOD;
        if(i & 1) ans = (ans - temp + MOD) % MOD;
        else ans = (ans + temp) % MOD;
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/pengwill97/article/details/80876150
今日推荐