Codeforces 437B & 437C

Codeforces 437B

题意:在1到limit之间找最少个数,使得他们的lowbit总和等于sum。

先求出全部的lowbit()进行排序,然后扫一遍即可。

#include<iostream>
#include<algorithm>
using namespace std;
struct node{
    int n,ln;
}a[100005];
int lowbit(int x)
{
    return x&(-x);
}
bool cmp(node aa,node bb)
{
    return aa.ln>bb.ln;
}
int can[100005];
int main()
{
    int sum,limit,ok=0,s=0;
    long long tot=0;
    cin >> sum >> limit;
    for(int i=0;i<limit;i++)
    {
        a[i].n=i+1;
        a[i].ln=lowbit(i+1);
    }
    sort(a,a+limit,cmp);
    for(int i=0;i<limit;i++)
    {
        if(tot+a[i].ln < sum)
        {
            can[i]=1;
            tot+=a[i].ln;
            s++;
            continue;
        }
        if(tot+a[i].ln == sum)
        {
            can[i]=1;
            tot+=a[i].ln;
            s++;
            break;
        }
        if(tot>sum)
        {
            can[i]=0;
            continue;
        }
    }
    if(tot==sum)    ok=1;
    if(ok)
    {
        cout << s << endl;
        for(int i=0;i<limit;i++)
        {
            if(can[i])
                cout << a[i].n << " ";
        }
        cout << endl;
    }
    else
    {
        cout << "-1" << endl;
    }
    return 0;
} 

Codeforces 437C

题意:n个部分,m个连接线,拆某根线需要花费能量,为其两端连接部分的能量值中的较小值。求要把所有连接线拆掉所需最小花费。

无脑边读边加,线肯定是要全拆掉的,读一个累加一次最后输出即可。

#include<iostream>
#include<algorithm>
using namespace std;
int a[1005];
int main()
{
    int n,m;
    long long ans=0;
    cin >> n >> m;
    for(int i=1;i<=n;i++)
    {
        cin >>a[i];
    }
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin >> x >> y;
        ans+=min(a[x],a[y]);
    }
    cout << ans <<endl;
    return 0;
} 
时间: 2024-10-08 20:17:54

Codeforces 437B & 437C的相关文章

CodeForces 437B The Child and Set

Description CodeForces 437B 描述 从 $1\sim limit$ 中选取若干个互不相同的数字,使得这些数字的 $lowbit$ 和等于 $sum$,$1\leq sum, limit\leq 10^5$ . 输入 两个数 $sum, limit(1\leq sum,limit\leq 10^5)$. 输出 第一行输出 $n$ 表示所选数字的数目,然后第二行 $n$ 个数,表示所选数字. 如果无解输出$-1$. 样例 输入1 5 5 输出1 24 5 输入2 4 3 输

Codeforces 437B The Child and Set (贡献+贪心)

链接:https://codeforces.com/contest/437/problem/B 题意:给出n,sum,构造一个序列满足:整数,无重复,范围1-n,sigma(ai)=sum,输出长度和各个元素.n<1e5 题解:考虑1-n的每个整数对sum的贡献,在从大往下取(贪心). #include <bits/stdc++.h> using namespace std; typedef long long ll; #define lowbit(x) ((x)&-(x)) c

笔记:lowbit()

因为CodeForces 437B这道题,接触到了lowbit. 先写一下函数: int lowbit(int x) { return x&(-x); } 以及原题上给出的定义: lowbit(x) equals 2k where k is the position of the first one in the binary representation of x. For example, lowbit(100102) = 102, lowbit(100012) = 12, lowbit(1

Codeforces 437C The Child and Toy(贪心)

题目连接:Codeforces 437C The Child and Toy 题目大意:孩子有一个玩具,有n个部件组成,m条绳子组成,每条绳子连接两个部件.小孩比较顽皮,要将玩具拆成不可分割的部件,每次剪断一条绳子的代价是该绳子连接的两个部件的权值中较小的值.问说最小的总代价是多少. 解题思路:以为每条边都是要被剪断的,所以将节点按照代价值从大到小排序,每次拿掉权值大的点,与该点连接并且还未剪断的边均用另外点的权值. #include <cstdio> #include <cstring

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store