当前位置: 首页 > news >正文

【CF】Day15——Codeforces Round 1012 (Div. 2) CD

C. Dining Hall

题目:

 

思路:

题目不难,但是读题困难了

题意缩减一下就是1是社牛,只要离得近并且有位置,那就直接坐,0是社恐,必须要离得近的同时这一桌都没人才坐

那我们就可以分析出来,0只可能坐到一个桌子的左下角,而1可以随便坐,那我们按题意模拟即可

由于n最多50000,那么我们最多只需要计算到 n*3 的坐标即可(观察图中例子可知其坐标是3倍的关系递增)

那么如何模拟呢?我们可以用set来储存桌子,我们可以将坐标和距离打包在一个pair中,这样自动排序的时候就会按距离先排了,如果距离相同再按x小的排

特别的,由于0只坐左下角,为了方便我们可以给他专门开个set存左下角的位置

注意,每次使用后记得删除

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"
void solve()
{
    int n;
    cin >> n;
    int len = 3 * n;
    set<pair<int, pair<int, int>>> s0, s1;
    pair<int, pair<int, int>> nowpos;
    for (int x = 0; x <= sqrt(len); x++)
    {
        for (int y = 0; y <= sqrt(len); y++)
        {
            int dis = 3 * (x + y) + 2;
            int newx = 3 * x + 1;
            int newy = 3 * y + 1;
            s0.insert({ dis,{newx,newy} });
            s1.insert({ dis,{newx,newy} });
            s1.insert({ dis + 1,{newx,newy + 1} });
            s1.insert({ dis + 1,{newx + 1,newy} });
            s1.insert({ dis + 4,{newx + 1,newy + 1} });
        }
    }
    vector<int> t(n);
    for (int i = 0; i < n; i++)
    {
        cin >> t[i];
    }
    for (int i = 0; i < n; i++)
    {
        if (t[i])
            nowpos = *s1.begin();
        else
            nowpos = *s0.begin();
        cout << nowpos.second.first << " " << nowpos.second.second << endl;
        s0.erase(nowpos);
        s1.erase(nowpos);
    }
}

signed main()
{
    cin.tie(0)->sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

D. Simple Permutation

题目:

思路:

喜闻乐见的构造题,脑电波对上了,但么写出

题目让我们构造出一个排列使得满足对于 c 集合存在至少 [n/3] - 1个元素,其中 c[i] 代表的是前 i 个元素的和除以 i ,即 c[i] = Sum(p[1]~p[i]) / i

这题有两个方法,先说我一开始想的

①.我们观察样例可以知道,我们要构造出素素,那么肯定是往小的素数上构造,因为越往后素数越大,离得也越远,那我们肯定要构造出一个这样的数列 c ,其形式如 2 3 3 3 5 5 5 7 7 7...

比如最后一个样例是 2 1 3 4 5,那么如果我们后面要构造的话肯定只能构造5了,因为就算我们选的数是6,我们最小只能获得4,这显然不利于我们构造,所以我们就应该看看能不能构造出5来,显然是可以的,但是前提是我们能取到,但是方法我感觉有点复杂了。。。

②.如果我们观察仔细的话,我们可以观察到题目的要求是 n/3 ,我们来想一想这有什么特性吗?

欸,我们可以发现,如果我们把排列分成 3 等份,那么我只要在中间这等分里找到一个素数 p,那么肯定能在左右两边都拿出两个数来组成素数 p 的两倍,比如如果我们选7,那么我们就可以选6 8构成 7 6 8,这样就有两个素数了,也就是说我们可以将两个数变成一个素数

那么我们两侧都有n/3个数,那么我们起码能构造出n/3个素数,只要我们在中间任选一个素数,这一定是可以构造出来的

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"

int isp[100005];

void prework()
{
    isp[0] = isp[1] = 0;
    for (int i = 2; i <= 100000; i++)
    {
        isp[i] = 1;
        for (int j = 2; j <= sqrt(i); j++)
        {
            if (i != j && i % j == 0)
                isp[i] = 0;
        }
    }
}

void solve()
{
    int n;
    cin >> n;
    int p = 0;
    for (int i = n/3; i <= n*2/3; i++)
    {
        if (isp[i])
        {
            p = i;
            break;
        }
    }
    if (p == 0)
    {
        for (int i = 1; i <= n; i++)
            cout << i << " ";
        cout << endl;
    }
    else
    {
        cout << p << " ";
        int l = p - 1, r = p + 1;
        while (l || r <= n)
        {
            if (l) {
                cout << l << " ";
                l--;
            }
            if (r <= n)
            {
                cout << r << " ";
                r++;
            }
        }
        cout << endl;
    }
}

signed main()
{
    prework();
    cin.tie(0)->sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

相关文章:

  • 【微服务架构】故障转移策略的理解及手写实现
  • C++ STL 序列式容器之(三)-- List
  • 【模型压缩+推理加速】知识蒸馏综述解读
  • 第四章 异常处理
  • 基于大模型的结核性胸膜炎风险预测及临床方案研究报告
  • CF2041C Cube
  • 《Operating System Concepts》阅读笔记:p481-p482
  • stanley 路径跟踪控制算法
  • 从概率到梯度:理解分类问题中交叉熵的优越性
  • 竞品已占据市场先机,如何找到差异化突破口
  • IT监控知识库:构建智能运维的认知中枢
  • idea激活后一直出现弹窗解决办法
  • 银行分布式新核心的部署架构(两地三中心)
  • 【实战ES】实战 Elasticsearch:快速上手与深度实践-2.2.1 Bulk API的正确使用与错误处理
  • 小爱控制OK影视搜索视频
  • 《Python实战进阶》第33集:PyTorch 入门-动态计算图的优势
  • 正学传承人——理行
  • Langchain RAG介绍和最佳实践
  • 突破反爬困境——SDK架构设计,为什么选择独立服务模式(四)
  • Qt中10倍提升动态截屏及渲染60帧/秒
  • 国际学院网站建设的意义/阿里云模板建站
  • 网站开发适合什么工作/网络营销案例有哪些
  • 广州响应式网站建设/公司网络营销实施计划
  • 简约大气网站设计欣赏/在百度上怎么发布信息
  • 做网站赚钱但又不想开公司/怎么找到精准客户资源
  • 服装行业网站建设及推广/搜索引擎优化专员