首页 > 其他分享 >E - Kth Takoyaki Set

E - Kth Takoyaki Set

时间:2023-04-09 22:23:01浏览次数:34  
标签:Takoyaki typedef Set int pq vector Kth visited include

E - Kth Takoyaki Set

https://atcoder.jp/contests/abc297/tasks/abc297_e

 

思路

使用优先队列,从0 开始, 对所有可能的扩展,计算累加和, 添加到队列,

每次从队列取出最小值,直到取出第k个。

Code

#include <iomanip>
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#include <limits.h>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = { -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8] = { 0, 1, -1, 0, -1, 1, -1, 1 };

#ifdef TESTING
#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define DEBUG
#define VALUE(x)
#define debug(...)
#endif

#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)

/******************************** COMMON FUNC START ***************************************/

inline string IntToString(LL a)
{
    char x[100];
    sprintf(x, "%lld", a);
    string s = x;
    return s;
}

inline LL StringToInt(string a)
{
    char x[100];
    LL res;
    strcpy(x, a.c_str());
    sscanf(x, "%lld", &res);
    return res;
}

inline string GetString(void)
{
    char x[1000005];
    scanf("%s", x);
    string s = x;
    return s;
}

inline string uppercase(string s)
{
    int n = SIZE(s);
    REP(i, n)
    if (s[i] >= 'a' && s[i] <= 'z')
        s[i] = s[i] - 'a' + 'A';
    return s;
}

inline string lowercase(string s)
{
    int n = SIZE(s);
    REP(i, n)
    if (s[i] >= 'A' && s[i] <= 'Z')
        s[i] = s[i] - 'A' + 'a';
    return s;
}

inline void OPEN(string s)
{
#ifndef TESTING
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
#endif
}

/******************************** COMMON FUNC END ***************************************/

/******************************** GRAPH START ***************************************/
// Graph class represents a directed graph
// using adjacency list representation
class Graph {
public:
    map<int, bool> visited;
    map<int, list<int> > adj;

    // function to add an edge to graph
    void addEdge(int v, int w);

    // DFS traversal of the vertices
    // reachable from v
    void DFS(int v);
};

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}

void Graph::DFS(int v)
{
    // Mark the current node as visited and
    // print it
    visited[v] = true;
    cout << v << " ";

    // Recur for all the vertices adjacent
    // to this vertex
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFS(*i);
}

/******************************** GRAPH END ***************************************/

/*
https://atcoder.jp/contests/abcxxx/tasks/abcxxx_d
*/

LL n, k;
vector<LL> a;
map<LL, bool> visited;
priority_queue<LL, vector<LL>, greater<LL> > pq;

int main()
{
    cin >> n >> k;

    for(int i=0; i<n; i++){
        LL temp;
        cin >> temp;
        a.push_back(temp);
    }

    sort(a.begin(), a.end());

    pq.push(0);
    visited[0] = true;

    LL order = -1;
    while(!pq.empty()){
        LL first = pq.top();
        pq.pop();

//        cout << "first = " << first << endl;

        order++;
//        cout << "order = " << order << endl;
        
        if (order == k){
//            cout << "found!" << endl;
            
            cout << first << endl;
            break;
        }
        
        for(int i=0; i<a.size(); i++){
            LL gain = a[i];
            LL next = first + gain;
            
            if (visited[next]){
                continue;
            }
            
//            cout << "next ="<< next << endl;

            visited[next] = true;

            pq.push(next);
        }
    }

    return 0;
}

 

标签:Takoyaki,typedef,Set,int,pq,vector,Kth,visited,include
From: https://www.cnblogs.com/lightsong/p/17301267.html

相关文章

  • JAVA实体类-自定义Getter Setter
    ###案例一整个购物车存放的商品信息需要计算的属性需要重写get方法,保证每次获取属性都会进行计算privateBigDecimaltotalPrice;/***计算当前购物项总价*@return*/publicBigDecimalgetTotalPrice(){//等于单价*数量returnthis.price.multiply(......
  • 第136篇:Three.js基础入门动画API:setInterval 与 requestAnimationFrame的区别
    好家伙,书接上文 functionanimate(){//请求-动画-框架requestAnimationFrame(animate);//改变正方体在场景中的位置,让正方体动起来cube.rotation.x+=0.01;cube.rotation.y+=0.01;renderer.render(......
  • C# WinForm操作配置文件AppSettings获取、增加、删除、修改
    在C#WinForm开发中,如果想要修改AppSettings中的值,发现用下面这个代码并没有成功。ConfigurationManager.AppSettings.Set(key,value);//修改值,但是没有成功下面提供可以用的获取、增加、删除、修改appSettings的方法。publicclassWinConfigHelper{///<summary>......
  • 关于开发环境中的charset问题
    中文的最大麻烦就是不同charset在实际的字节存储是不同的。而Windows的缺省为GBK,Linux的缺省为UTF-8。一个汉字的GBK中的存贮在2个字节,在UTF-8中存贮在3个字节,如果字符集不统一,就会出现显示乱码的现象,如果设计到数据库的存储,问题就更大。一般而言,不同的程序相互交互,一般会使用更为......
  • keil 5 stm32f4 固件库 set up文件链接
    STSW-STM32065-STM32F4DSP和标准外设库-意法半导体STMicroelectronics ......
  • Vulnhub Bravery靶机 Walkthrough
    BraveryRecon使用netdiscover对本地网络进行arp扫描。┌──(kali㉿kali)-[~]└─$sudonetdiscover-r192.168.80.0/24Currentlyscanning:Finished!|ScreenView:UniqueHosts5......
  • JDBC-API详解--ResultSet
    ResultSet作用:1.封装查询语句ResultSetexecuteQuery(sql):执行查询语句,返回ResultSet对象。·获取查询结果:booleannext();  1将光标从当前位置向前移动一行2判断当前行是否为有效行。返回值:true为有效行 false为无效行XXXgetXxx(参数)用于获取数据参数:可以是int......
  • getter和setter方法的一些注意事项
    getter和setter方法的一些注意事项布尔型应该用isxxx()进行设置,而get方法不变class代码publicclassPerson{privateStringname;privatebooleanis_male;publicvoidsetName(Stringname){this.name=name;}publicStringgetNam......
  • setObject方法的作用
    setObjectsetObject就是给JDBC的SQL语句的占位符赋值的,即是下面的“?”预编译的SQL:参数使用?作为占位符注意:sql的参数使用?作为占位符。如:select*fromuserwhereusername=?andpassword=?;1获取执行sql语句的对象PreparedStatementConnection.prepareStatement(S......
  • django中使用orm连接mysql,setting.py的设置
    默认使用的时sqllite数据库,我们需要改成mysql,只要需要填写相关信息即可。比如mysql的数据库名,用户名,密码,主机地址,端口等信息#Database#https://docs.djangoproject.com/en/4.1/ref/settings/#databases#DATABASES={#'default':{#'ENGINE':'django.db.b......