CF 367C Sereja and the Arrangement of Numbers(欧拉路径)

题意:给出m个不同的数,并且每个数都有个费用,现在要在m个数中选择一些数,用这些数组成一个长度为n的数列,并且满足任意两个不同种类的数都相邻。问最大的费用是多少。

思路:不同种数的区别只有费用,不妨按费用从大到小排序,现在就是要在前几个数中选一些满足要求。可以把它想象成一个图,每个数代表一个顶点,两个数相邻代表两个顶点之间有条边,根据题中的要求,构造出来的图就是完全图。做题的时候没想那么多,找了找规律A掉了,AC后看题解才发现这题其实很巧妙。这个数列可以看成一个路径,每在当前数列后加一个数就相当于连了一条边,这样这题就变成了找一条欧拉路径!顶点为奇数的时候一定存在欧拉路径,因为每个顶点的度数都为偶数,顶点为偶数的时候要添加一些边来满足要求,添加的边数即为n/2-1。有趣的题~


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=10000+10;
const int Maxn=100000+10;
ll a[maxn];
int w[Maxn];
bool cmp(int x,int y) { return x>y;}
void Init()
{
    for(ll i=1;i<maxn;++i)
    {
        if(i&1) a[i]=i*(i-1)/2+1;
        else a[i]=i*i/2;
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m,tmp;
    Init();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i)
        scanf("%d%d",&tmp,&w[i]);
    sort(w+1,w+m+1,cmp);
    int p=0;
    for(int i=1;i<maxn;++i)
        if(a[i]<=n) p=i;
        else break;
    ll ans=0;
    for(int i=1;i<=min(m,p);++i)
        ans+=w[i];
    printf("%I64d\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
An LCD (Liquid Crystal Display) is a type of display technology that uses liquid crystals to display information. LCDs are commonly used in electronic devices such as digital watches, calculators, and mobile phones, and can also be used in more complex systems such as embedded systems and microcontrollers. A matrix keypad, on the other hand, is a type of input device that allows users to input data through a combination of buttons arranged in a matrix pattern. A typical matrix keypad has rows and columns of buttons that can be pressed to input data. The arrangement of the buttons in a matrix pattern allows for a large number of buttons to be accommodated in a small space. To interface an LCD and a matrix keypad with a microcontroller, several specifications need to be considered. These include: 1. Pinout: The pinout of the LCD and matrix keypad needs to be understood in order to connect them to the microcontroller. The pinout typically includes pins for power, ground, data, and control signals. 2. Voltage levels: The voltage levels of the LCD and matrix keypad need to be compatible with the microcontroller. Most microcontrollers operate at 5V or 3.3V, so the LCD and matrix keypad should also operate at these voltage levels. 3. Data communication protocol: The data communication protocol between the microcontroller, LCD, and matrix keypad needs to be understood in order to transfer data between them. The most common data communication protocols used are SPI (Serial Peripheral Interface) and I2C (Inter-Integrated Circuit). 4. LCD commands: The LCD has a set of commands that can be used to control its behavior, such as clearing the screen, setting the cursor position, and displaying text. These commands need to be understood in order to control the LCD using the microcontroller. 5. Matrix keypad scanning: The matrix keypad needs to be scanned in order to detect which buttons are being pressed. This is typically done using a technique called matrix scanning, where the rows and columns of the keypad are scanned sequentially to detect button presses. Overall, interfacing an LCD and matrix keypad with a microcontroller requires a thorough understanding of their specifications and how they can be connected and controlled.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值