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;
}