codeforces 250 div2 A B C D

A. 模拟

#include <bits/stdc++.h>

using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define maxn 100 + 10

char s[4][maxn];
int len[4];

int main()
{
    int t1, t2;
    int Min = INF, Max = -INF;
    for(int i = 0; i < 4; i++)
    {
        scanf("%s", s[i]);
        len[i] = strlen(s[i]) - 2;
        //cout<<len[i]<<endl;
        if(Max < len[i])
        {
            Max = len[i];
            t1 = i;
        }
         if(Min > len[i])
        {
            Min = len[i];
            t2 = i;
        }
    }

    int t = 0;
    int ans;
    int i ;
    for(i = 0; i < 4; i++)
    {
        if(i != t1 && Max < 2 * len[i])
        {
            break;
        }
    }
    if(i == 4)
    {
        t++;
        ans = t1;
    }

    for(i = 0; i < 4; i++)
    {
        if(i != t2 && 2 * Min > len[i])
        break;
    }
    if(i == 4)
    {
        t++;
        ans = t2;
    }
    if(t == 2 || t == 0)
    printf("C\n");
    else printf("%c\n", 'A' + ans);
    return 0;
}

/*

A._
B.__
C.____
D.________

*/

B. 推理

#include <bits/stdc++.h>

using namespace std;

int lowbit(int x)
{
    return x & ( -x);
}

int n, m;
int a[100005];
int cnt = 0;

int main()
{
    cin>>n>>m;
    for(int i = m; i >= 1; i--)
    {
        if(lowbit(i) <= n)
        {
            n -= lowbit(i);
            a[cnt++] = i;
        }
        if(n == 0) break;
    }
    if(n != 0) printf("-1\n");
    else
    {
        cout<<cnt<<endl;
        for(int i = 0; i < cnt; i++)
    {
        i == cnt - 1 ? printf("%d\n", a[i]) : printf("%d ", a[i]);
    }
    }
    return 0;
}

C. 推理

#include <bits/stdc++.h>

using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define maxn 100000 + 10

int v[maxn];
int n, m;

int main()
{
    cin>>n>>m;
    for(int i = 1; i <= n ; i++)
    scanf("%d", v + i);

    long long ans = 0;
    for(int i = 0; i < m; i++)
    {
        int uu, vv;
        scanf("%d%d", &uu, &vv);
        ans += min(v[uu], v[vv]);
    }
    printf("%I64d\n", ans);
    return 0;
}

D. 并查集

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
#define N 100000 + 10

struct Edge
{
    int u, v, w;
}e[N];

int n, m;
int w[N];
int f[N], cnt[N];
double ans = 0;

int cmp(Edge a, Edge b)
{
    return a.w > b.w;
}

void mekeset()
{
    for(int i = 1; i <= n; i++)
    {
        f[i] = i;
        cnt[i] = 1;
    }
}

int Find(int x)
{
    return x == f[x] ? x : f[x] = Find(f[x]);
}

int Union(int i, int x, int y)
{
    int xx = Find(x);
    int yy = Find(y);
    if(xx != yy)
    {
       ans += (double)e[i].w * cnt[xx] * cnt[yy];
       cnt[xx] += cnt[yy];
       f[yy] = xx;
    }
}

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", w + i);
    }
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d", &e[i].u, &e[i].v);
        e[i].w = min(w[e[i].u], w[e[i].v]);
    }
    sort(e, e + m, cmp);

    mekeset();
    for(int i = 0; i < m; i++)
    {
        Union(i, e[i].u, e[i].v);
    }
    ans = ans * 2 / ((double)n *(n - 1));
    printf("%.6lf\n", ans);
    return  0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-02 02:47:36

codeforces 250 div2 A B C D的相关文章

codeforces Round #250 (div2)

a题,就不说了吧 b题,直接从大到小排序1-limit的所有数的lowbit,再从大到小贪心组成sum就行了 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #define N 200000 6 using namespace std; 7 int pos[N],a[N],s[N],f[N],la[N],b[N],i,j,k,ans,n,p

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

Codeforces #180 div2 C Parity Game

// Codeforces #180 div2 C Parity Game // // 这道题的题目意思就不解释了 // // 题目有那么一点难(对于我而言),不多说啦 // // 解题思路: // // 首先如果a串和b串相等,不多说直接YES // 如果b串全是0,直接YES // 注意到a串有一个性质,1的个数不会超过本身的加1. // a有个1的上限设为x,b有个1的个数设为y,则如果x < y // 那么直接NO. // // 现在一般情况下,就是模拟啦,找到a的后缀和b的前缀一样的

Codeforces #246(div2)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <ti

Codeforces #245(div2)

A:A. Points and Segments (easy) 题目看了n久,开始觉得尼玛这是div2的题目么,题目还标明了easy.. 意思是给你一n个点,m个区间,在n个点上放蓝球或者红球,然后让你找一种选择方案使得m个区间内的蓝球和红球数量之差不超过1. 开始想过用dfs,不过这只是div2的A题而已.. 然后想了下,直接输出010101序列不就可以么. 交了一发,发现要先排个序,再输出就可以了. AC代码: #include<iostream> #include<cstdio&g

codeforces#327 div2

codeforces#327 div2 这场状态不好有点可惜,题目都不难,而且很好.. A题:水题. #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; typedef lo

codeforces#FF(div2) DZY Loves Sequences

n个数,可以任意改变其中一个数,求最长的上升子区间长度 思路:记录一个from[i]表示从位置i的数开始最长的上升区间长度 记录一个to[i]表示到位置i的数所能达到的最长上升区间长度 枚举要改变的数的位置i,此时能达到的长度为to[i - 1] + from[i + 1] + 1,取最大值 //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #include <cstdio> #include &l

Codeforces #250 (Div. 2) C.The Child and Toy

之前一直想着建图...遍历 可是推例子都不正确 后来看数据好像看出了点规律 就抱着试一试的心态水了一下 就....过了..... 后来想想我的思路还是对的 先抽象当前仅仅有两个点相连 想要拆分耗费最小,肯定拆相应权值较小的 在这个基础上考虑问题就能够了 代码例如以下: #include <cstdio> #include <iostream> #include <algorithm> #define MAXN 10010 #define ll long long usi

Codeforces 258 Div2

A题,n*m根木棍,相交放置,轮流取走相交的两根,最后谁不能行动,则输掉. min(n,m)&1 为1则先取者赢. B题,给定一个长度为n,且各不相同的数组,问能否通过交换连续一段L....R使得变成单调递增. 如果一开始就是递增的,那么直接输出L...R就是1 1,交换一个就行了:否则判断中间是否有且一段单调递减,且两端交换后使得数组递增. 代码: 1 //Template updates date: 20140718 2 #include <iostream> 3 #include