Test40普转提考试总结
Claire0630 · · 题解
Test40普转提考试总结
Data
Score:234 = 50 + 100 + 30 + 54
Rank: B组P6,全组P14
Preface
看似是提高组模拟赛,但线段树都不考那不叫一个纯正(国窖1573~~~)
老登张铭轶比我高一分..........>_<
T1写SPFA被卡爆了.......(忘记怎么写Dij的我要被挨打了:) )
T2好水
T3豪逆天(结论超出我的推理范围)
T4看似是版但却被坑爆了
T4数据好水(但我没有吃到红利啊啊啊啊啊)
T4写Kruskal板子有82!!!!???????(我离板子还差一步,排序没排完啊啊啊啊啊)
T4hack数据超模!!!!坑我100年~
Statements
T1.魔法传送门
在神秘的艾拉希尔大陆上,有
Input
第一行包含两个整数
第二行包含四个整数
接下来
Output
输出一行一个整数,表示可以让贤王满意的传送门建立方式的总数。
Examples
【输入输出样例#1】
| portal.in | portal.out |
|---|---|
| 7 8 | |
| 6 7 1 2 | |
| 1 2 1 | |
| 1 6 1 | |
| 2 3 1 | 4 |
| 2 4 1 | |
| 3 5 1 | |
| 3 7 1 | |
| 4 5 1 | |
| 5 6 1 |
【输入输出样例#2】
| portal.in | portal.out |
|---|---|
| 3 2 | |
| 1 3 1 2 | 3 |
| 1 2 1 | |
| 2 3 1 |
【输入输出样例#3】
| portal.in | portal.out |
|---|---|
| 6 4 | |
| 2 5 1000000000 1 | |
| 1 2 1000000000 0 | 0 |
| 2 3 1000000000 | |
| 2 4 1000000000 | |
| 5 6 1000000000 |
Data Scope and Agreements
| 子任务 | 限制条件 | 分值 |
|---|---|---|
| 第1种 | 30 | |
| 第2种 | 20 | |
| 第3种 | 20 | |
| 第4种 | 无其他限制 | 30 |
所有数据满足:
Solution
我们看到这道题会想到计数,这是显然的,同时还有最短路算法,我们这道题果断选择SPFADij,因为前者会被可恶的数据给卡爆
Dij模板
void dij(LL u){
for(LL i=1;i<=n;i++){
fl[i] = 0;
dis[i] = 1e18;
}
multiset<PLL> q;//priority_queue也行
q.clear();
q.insert((PLL){0,u});
dis[u] = 0;
while(q.size()){
PLL t = *(q.begin());
q.erase(q.begin());
if(fl[t.second] == 1) continue;
fl[t.second] = 1;
for(auto x : g[t.second]){
if(dis[x.dst] > t.first + x.val){
dis[x.dst] = t.first + x.val;
q.insert((PLL){dis[x.dst],x.dst});
}
}
}
return;
}
我们简单翻译题面,可以发现,题目就是想让我们求:增加一条长度为
我们画个图理解一下
于是我们直接从起点和终点跑两边最短路算法,记录好从起点和终点到每一个点的最短距离,存在
然后我们就枚举
时间复杂度
我们思考优化,对
可以通过
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
x = 0;
LL f = 1;
char c = getchar();
for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= f;
}
template <typename T> inline void print(T x){
if(x < 0) x = -x,putchar('-');
if(x > 9) print(x / 10);
putchar(x % 10 + 48);
}
const int N = 2e5 + 11,M = 25;
struct graph{
LL dst,val;
};
LL n,m,bg,ed,vl,k,ans;
vector<graph> g[N];
LL dis[5][N];
bool fl[N];
void dij(LL u){
int kw = 0;
if(u == bg) kw = 1;
else kw = 2;
for(LL i=1;i<=n;i++){
fl[i] = 0;
dis[kw][i] = 1e18;
}
multiset<PLL> q;
q.clear();
q.insert((PLL){0,u});
dis[kw][u] = 0;
while(q.size()){
PLL t = *(q.begin());
q.erase(q.begin());
if(fl[t.second] == 1) continue;
fl[t.second] = 1;
for(auto x : g[t.second]){
if(dis[kw][x.dst] > t.first + x.val){
dis[kw][x.dst] = t.first + x.val;
q.insert((PLL){dis[kw][x.dst],x.dst});
}
}
}
return;
}
int main(){
// freopen("portal.in","r",stdin);
// freopen("portal.out","w",stdout);
read(n);read(m);
read(bg);read(ed);read(vl);read(k);
for(LL i=1;i<=m;i++){
LL st,dst,val;
read(st);read(dst);read(val);
g[st].emplace_back((graph){dst,val});
g[dst].emplace_back((graph){st,val});
}
dij(bg);
if(dis[1][ed] <= k){
print(n * (n - 1) / 2);
return 0;
}
dij(ed);
sort(dis[1] + 1,dis[1] + 1 + n);
sort(dis[2] + 1,dis[2] + 1 + n);
for(LL i=1;i<=n;i++){
if(k - vl - dis[1][i] < 0) break;
LL pos = upper_bound(dis[2] + 1,dis[2] + 1 + n,k - vl - dis[1][i]) - dis[2] - 1;
ans += pos;
}
print(ans);
return 0;
}
T2.逻辑表达式
statement
给一个包含
本题中,输入的表达式满足以下限制:
- 保证是一个合法的后缀表达式,所有变量、运算符用空格隔开。
- 运算符包括与(&),或(|)两种。
- 变量由小写字母
x ) 与一个正整数拼接而成,正整数表示这个变量的下标。例如:x10 ,表示下标为 10 的变量。数据保证x1, x2, x3, ... 每个变量在表达式中出现恰好一次。
Input
第一行一个整数
每组数据第一行一个整数
第二行包含一个字符串
第三行包含
Output
每组数据输出一行一个整数代表答案。
Examples
| logic.in | logic.out |
|---|---|
| 5 | |
| 2 | |
| x1 x2 | |
| 11 | |
| 3 | |
| x1 x2 & x3 | 2 |
| 0 0 1 | 1 |
| 5 | 2 |
| x1 x2 x3 & x4 & x5 & & | 1 |
| 0 1 1 1 0 | 2 |
Data Scope and Agreements
对于 40% 的数据,
对于 60% 的数据,
对于另外 20% 的数据,
对于 100% 的数据,
Solution
其实也没啥好说的,水题一个,直接将原表达式建成表达式树即可,然后再在树上统计答案 Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
x = 0;
LL f = 1;
char c = getchar();
for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= f;
}
template <typename T> inline void print(T x){
if(x < 0) x = -x,putchar('-');
if(x > 9) print(x / 10);
putchar(x % 10 + 48);
}
const int N = 1e6 + 11,M = 25;
int t,n;
bool val[N],stat[N];
int cg[N];
stack<int> stk;
string s;
vector<int> g[N];
void dfs(int u){
if(g[u].size() == 0){
cg[u] = 1;
return;
}
int l = g[u].front(),r = g[u].back();
dfs(l);dfs(r);
if(stat[u] == 1){
if(val[l] == 1 && val[r] == 1) cg[u] = cg[l] + cg[r];
else if(val[l] == 0 && val[r] == 0) cg[u] = min(cg[l],cg[r]);
else{
if(val[l] == 1) cg[u] = cg[l];
if(val[r] == 1) cg[u] = cg[r];
}
}else{
if(val[l] == 0 && val[r] == 0) cg[u] = cg[l] + cg[r];
else if(val[l] == 1 && val[r] == 1) cg[u] = min(cg[l],cg[r]);
else{
if(val[l] == 0) cg[u] = cg[l];
if(val[r] == 0) cg[u] = cg[r];
}
}
}
void init(){
for(int i=1;i<=n*2;i++){
g[i].clear();
val[i] = 0;
cg[i] = 0;
stat[i] = 0;
}
while(stk.size()) stk.pop();
return;
}
int main(){
// freopen("logic.in","r",stdin);
// freopen("logic.out","w",stdout);
read(t);
while(t --){
read(n);
init();
getline(cin,s);
for(int i=1;i<=n;i++) read(val[i]);
int m = s.size(),id = n;
for(int i=0;i<m;i++){
if(s[i] == 'x'){
int j = i + 1,num = 0;
while(j < m && s[j] >= '0' && s[j] <= '9'){
num = num * 10 + s[j] - '0';
j ++;
}
stk.push(num);
i = j;
}else if(s[i] == '&'){
int b = stk.top();
stk.pop();
int a = stk.top();
stk.pop();
g[++ id].emplace_back(a);
g[id].emplace_back(b);
stk.push(id);
val[id] = (val[a] && val[b]);
stat[id] = 0;
}else if(s[i] == '|'){
int b = stk.top();
stk.pop();
int a = stk.top();
stk.pop();
g[++ id].emplace_back(a);
g[id].emplace_back(b);
stk.push(id);
val[id] = (val[a] || val[b]);
stat[id] = 1;
}
}
dfs(2 * n - 1);
print(cg[2 * n - 1]);
putchar('\n');
}
return 0;
}
T3.蚂蚁归巢
Statement
在一条笔直的木棍上,有两个蚁巢分别建在端点 0 和
第
蚂蚁们持续移动,直到发生下列事件之一:
- 如果一只蚂蚁到达了某个蚁巢(即 0 或
L ),则它会立即进入蚁巢并停止移动。 - 如果两只蚂蚁在木棍上的同一点(可能不是整数)相遇,且该点不是蚁巢,那么这两只蚂蚁会互换方向继续前进。
现在,定义
你的任务是,统计在时间区间
Input
第一行两个整数
接下来
Output
输出一个整数,表示
Example
| ant.in | ant.out |
|---|---|
| 3 5 | |
| 1 1 1 | 2 |
| 2 2 -1 | |
| 3 3 -1 |
Explanation
第 1 和第 2 只蚂蚁在
第 2 和第 3 只蚂蚁在
第 1 只蚂蚁在
第 2 只蚂蚁在
一共发生了两次相遇。
Data Scopes and Agreements
| 测试点 | 特殊性质 | ||
|---|---|---|---|
| 1-2 | 无 | ||
| 3-4 | 特殊性质A | ||
| 5 | 特殊性质B | ||
| 6 | 特殊性质C | ||
| 7-10 | 无 | ||
| 11-12 | 特殊性质A | ||
| 13 | 特殊性质B | ||
| 14 | 特殊性质C | ||
| 15-20 | 无 |
保证所有
特殊性质 A 为
特殊性质 B 为
特殊性质 C 为
Solution
Step1.整理核心性质
我们首先观察到,蚂蚁的碰撞这件事情对我们来说实在是太烦人了,我们不如换种方式思考问题
我们视蚂蚁间碰撞为蚂蚁间的“身份”交换,即方向不变但重量交换,我们就可以理解为所有蚂蚁的运动方向不做改变(性质1)
因为碰撞后实际反向,所以对于每一只蚂蚁,他们的相对位置不变(性质2)
因为我们视作身份交换,所以*对于一只蚂蚁现在在坐标轴上
Step2.计算截止时间T
我们根据前面的性质得到,蚂蚁的相对位置不变(性质2)和重量交换(性质1),我们继续分析,先根据每只蚂蚁的位置和朝向计算入巢时间
因为运动方向不会改变(性质1),所以
又因为第一只朝左的蚂蚁一定是第一只进入左巢穴的蚂蚁,第一只朝右的蚂蚁一定是第一只进入右巢穴的蚂蚁(不多介绍,感性证明即可,理性证明也没啥好说的)(你就可以理解为反弹多次又回去了)
所以我们使用优先队列(set也行)去存储每只蚂蚁的入巢时间
此时的
Step3.计算在[0,T] 以内的碰撞次数
我们先将朝左的蚂蚁和朝右的蚂蚁分别按位置从小到大的顺序存在两个不同的数组中
然后我们思考:
在
对于一只位置在
还需注意一点,两只蚂蚁碰面时一定是在两只蚂蚁入巢之前!!!!!
时间复杂度为
附上代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
x = 0;
LL f = 1;
char c = getchar();
for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= f;
}
template <typename T> inline void print(T x){
if(x < 0) x = -x,putchar('-');
if(x > 9) print(x / 10);
putchar(x % 10 + 48);
}
const int N = 5e4 + 11,M = 25;
LL n,l,tot;
struct node{
LL w,dir,x,id,t;
}g[N];
struct info{
LL t,dir;
};
class cmp2{
public:
bool operator()(const info x,const info y){
if(x.t > y.t) return 1;
return 0;
}
};
priority_queue<info,vector<info>,cmp2> q;
vector<node> vl,vr;
bool cmp(node x,node y){
if(x.x < y.x) return 1;
return 0;
}
int main(){
// freopen("ant.in","r",stdin);
// freopen("ant.out","w",stdout);
read(n);read(l);
for(LL i=1;i<=n;i++){
read(g[i].w);read(g[i].x);read(g[i].dir);
g[i].id = i;
tot += g[i].w;
if(g[i].dir == 1) g[i].t = l - g[i].x;
else g[i].t = g[i].x;
if(g[i].dir == 1) vr.emplace_back(g[i]);
if(g[i].dir == -1) vl.emplace_back(g[i]);
}
sort(g + 1,g + 1 + n,cmp);
if(vl.size() == n || vr.size() == n){
print(0);
return 0;
}
sort(vl.begin(),vl.end(),cmp);
sort(vr.begin(),vr.end(),cmp);
for(LL i=1;i<=n;i++){
if(g[i].dir == 1) q.push((info){l - g[i].x,1});
else q.push((info){g[i].x,-1});
}
LL hf = tot >> 1;
LL T = 0;
LL tow = 0,l = 1,r = n;
while(q.size()){
info t = q.top();
q.pop();
if(t.dir == -1){
tow += g[l].w;
l ++;
}else{
tow += g[r].w;
r --;
}
if(tow >= hf){
T = t.t;
break;
}
}
LL cnt = 0;
for(auto x : vr){
auto pos1 = upper_bound(vl.begin(),vl.end(),x,cmp);
node tmp = x;
tmp.x += 2 * T;
auto pos2 = upper_bound(vl.begin(),vl.end(),tmp,cmp);
for(auto it=pos1;it!=pos2;it++){
LL ct = it -> x - x.x;
if(ct <= x.t * 2 && ct <= (it -> t) * 2) cnt ++;
}
}
print(cnt);
return 0;
}
T4.能量传输
Statement
在星球 Zenthos 上,有 1, 2, ..., N 号的太空基地。这些基地之间有 M 条传输通道,能够在两个基地之间进行能量传输。遗憾的是,只有 1 号基地配备有完整的能量处理核心,其他基地必须通过它进行能量中转。
每条传输通道可能是开启的,也可能是关闭的。一个传输方案被称为有效的,如果开启的传输通道能够让 1 号基地与所有其他基地直接或间接连通。
当前,Zenthos 正在使用一组共 N - 1 条开启的传输通道,这显然是一个有效的方案。但是维护这些通道的开销非常昂贵。每条开启的传输通道都有一个月度维护费用。(未开启的传输通道不需要维护费用)
好消息是,科研机构最近研发了一种能量调控装置,可以用在某条传输通道上,使其维护费用从
你的任务是逐步替换传输通道(每天可以开启一条未开启通道并关闭一条已开启通道),最终形成一个新的有效方案,且总维护费用最小。
你可以在任意时刻使用调控装置,但最多只能用一次,且只能用在一条通道上。中间过程可以是不连通的,但最终方案必须有效。
请计算,至少需要多少天,才能完成该目标?
Input
第一行输入三个整数
接下来
保证前
Output
输出一个整数,表示至少需要多少天才能完成任务(即:在完成全部操作后获得总花费最小的有效通道方案)。
如果当前使用的方案已经是最优的,输出 0。
【输入输出样例#1】
| energy.in | energy.out |
|---|---|
| 4 4 0 | |
| 1 2 1 | |
| 2 3 2 | 1 |
| 3 4 1 | |
| 4 1 1 |
【输入输出样例#2】
| energy.in | energy.out |
|---|---|
| 4 4 0 | |
| 1 2 715827882 | |
| 2 3 715827882 | 0 |
| 3 4 715827882 | |
| 4 1 715827884 |
【输入输出样例#3】
| energy.in | energy.out |
|---|---|
| 5 6 2 | |
| 1 2 5 | |
| 2 3 5 | |
| 1 4 5 | 2 |
| 4 5 5 | |
| 1 3 1 | |
| 1 5 1 |
Explanation
样例1:
推进器强度为0,所以无法降低任何通道费用。
替换方案:关闭通道2-3,开启通道4-1,总花费更小。仅需1天。
样例2:
当前方案已经是最优,无需更改,输出0。
样例3:
首先在通道1-2上使用推进器,将其费用从5降为3。
第1天替换2-3为1-3。
第2天替换1-4为1-5,得到更优方案,总成本从25降为10,所需天数为2。
Data Scopes and Agreements
对于20%的数据,
对于40%的数据,
对于另外20%的数据,
对于另外20%的数据,
对于100%的数据,
Solution
显而易见,这道题使用Kruskal
我们考虑最后情况的满足条件:
1.必须保证费用最优
2.必须保证修改路径最少(即更多的边为原始边)
那么我们先生成最小生成树,然后枚举MST没有使用到的边,如果其中有一条的费用更优且是原始边,我们就替换其,然后break即可
这道题坑点很多,要注意代码细节处理
这里给两组HACK数据
Hack1.in
6 7 12
1 3 15
1 2 8
2 4 1
4 5 3
5 6 11
3 2 9
4 6 4
Hack1.out
2
Hack2.in
7 12 5
1 3 100
3 5 5
5 7 100
7 2 100
2 4 100
4 6 100
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
6 7 1
Hack2.out
5
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
x = 0;
LL f = 1;
char c = getchar();
for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= f;
}
template <typename T> inline void print(T x){
if(x < 0) x = -x,putchar('-');
if(x > 9) print(x / 10);
putchar(x % 10 + 48);
}
const int N = 2e5 + 11,M = 25;
struct edge{
LL st,dst,val,id;
};
struct DSU{
vector<LL> par;
DSU(LL n){
par.resize(n + 1);
for(LL i=1;i<=n;i++) par[i] = i;
}
LL find(LL u){
if(par[u] != u) return par[u] = find(par[u]);
else return u;
}
void comb(LL u,LL v){
LL rtu = find(u),rtv = find(v);
if(rtu == rtv) return;
par[rtv] = rtu;
}
bool connected(LL u,LL v){
if(find(u) == find(v)) return 1;
else return 0;
}
};
LL n,m,d;
vector<edge> v;
bool fl[N];
bool cmp(edge x,edge y){
if(x.val < y.val) return 1;
if(x.val == y.val && x.id < y.id) return 1;
return 0;
}
int main(){
// freopen("energy.in","r",stdin);
// freopen("energy.out","w",stdout);
read(n);read(m);read(d);
for(LL i=1;i<=m;i++){
LL st,dst,val;
read(st);read(dst);read(val);
v.emplace_back((edge){st,dst,val,i});
}
DSU unit(n);
sort(v.begin(),v.end(),cmp);
LL ans = 0,pos = 0;
for(LL i=0,cnt=1;i<m&&cnt<=n-1;i++){
if(unit.connected(v[i].st,v[i].dst) == 1) continue;
unit.comb(v[i].st,v[i].dst);
cnt ++;
fl[i] = 1;
if(v[i].id >= n) ans ++;
pos = i;
}
DSU nunit(n);
for(LL i=0;i<pos;i++){
if(fl[i] == 1 && v[i].val < v[pos].val) nunit.comb(v[i].st,v[i].dst);
}
for(LL i=pos+1;i<m;i++){
if(nunit.connected(v[i].st,v[i].dst) == 1) continue;
if(v[i].val <= d && v[i].id < n){
ans --;
break;
}else break;
}
print(ans);
return 0;
}