2024.9.8 模拟赛
T1
现在你站在坐标轴的一个整点上,给你
随便玩了一下,感觉需要所有学会的
有一个 DP 的想法是把值开进状态里去转移。
感觉这玩意值不是很多的样子,起码超不过所有的因数和,
吐槽一下 imposib1e,虽然提醒了但是什么玩意。
//%^~
#include <bits/stdc++.h>
#define INF 0x7fffffff
#define MAXN 100005
#define eps 1e-9
#define foru(a,b,c) for(int a=b;a<=c;a++)
#define RT return 0;
#define LL long long
#define LXF int
#define RIN rin()
#define HH printf("\n")
#define All(x) (x).begin(),(x).end()
#define fi first
#define se second
using namespace std;
inline LXF rin(){
LXF x=0,w=1;
char ch=0;
while(ch<'0'||ch>'9'){
if(ch=='-') w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+(ch-'0');
ch=getchar();
}
return x*w;
}
int n;
pair<int,int> a[MAXN];
unordered_map<int,int> f[1005];
signed main(){
n=RIN;
foru(i,1,n){
a[i].fi=RIN;
// a[i].fi=73513440;
}
foru(i,1,n){
a[i].se=RIN;
}
// sort(a+1,a+1+n,[&](pair<int,int> p,pair<int,int> q){
// return p.se<q.se;
// });
int ans=INT_MAX;
// foru(i,1,n){
// foru(j,i+1,n){
// // if(__gcd(a[i].fi,a[j].fi)==1){
// // ans=min(ans,a[i].se+a[j].se);
// // }
// if(a[i].se+a[j].se==3327){
// cout<<a[i].fi<<' '<<a[i].se<<endl;
// cout<<a[j].fi<<' '<<a[j].se<<endl;
// }
// }
// }
// foru(i,1,n){
// for(int i=1;i*i<=a[i])
// }
f[0][0]=0;
foru(i,1,n){
for(auto [x,y]:f[i-1]){
int g=x;
if(f[i].find(g)==f[i].end()) f[i][g]=INT_MAX;
f[i][g]=min(f[i][g],f[i-1][x]);
g=__gcd(a[i].fi,x);
if(f[i].find(g)==f[i].end()) f[i][g]=INT_MAX;
f[i][g]=min(f[i][g],f[i-1][x]+a[i].se);
}
}
if(f[n].find(1)==f[n].end()){
ans=INT_MAX;
}else{
ans=f[n][1];
}
if(ans==INT_MAX){
cout<<"imposib1e";
}else{
cout<<ans;
}
return 0;
}
T2
笨比的一题
从
被 T1 做昏了,上来就 DP 了。
定义
满足这些条件并不一定没有缝,事实上只要
i 总体上是在j 上方一个区域内就符合要求。但我们是要从1 一直连到n ,是一个单向的过程,同时注意到我们的式子中i 和j 并不对称,因此即使出现非法边也是不优的。
然后我们容易想到把每个三角形看做一个点,点权是
暴力连边需要
点权最短路有一个性质是,跑 dijkstra 的时候,一个点只会被松弛一次。换句话说,一个点从
因此,我们可以让一个点在更新
有没有什么办法不访问到无效的建边尝试呢?
我们来变形一下刚才的式子。
发现两侧分别只和
数据结构优化建图堂堂登场
可以想到用线段树套值域线段树来优化二维的建图,但这样空间需要
但我们真的需要使用平衡树吗?现在我们有 set 里二分定位到其内部的一个区间来获得所有后继,并需要 vector 来降序存储第二维的元素,使用 pop_back 来
哪怕需要前后缀删除,我们也可以使用
deque,不过这位兄弟的常熟并不理想,实测不如set,甚至某些情况下劣于set。
这其中有隐式建图的思想。既然数据结构内部的边都无权值,我们干脆不建边,只是把数据结构当成 ”找后继的工具“ 即可。同时这样做也有一个好处,一旦我们在数据结构内删除了某个点,那么我们就瞬间删除了它的所有入度,这是链式前向星等建图方式所不能具备的。
其实会出现当前在
u 点,来到线段树上查后继,查到的后继已经被删除。这是因为每个点都被复制了\log 份。不过这并不影响,因为我们会判断dis_v 是否已经是最短路。
//%^~
#include <bits/stdc++.h>
#define INF 0x7fffffff
#define MAXN 100005
#define eps 1e-9
#define foru(a,b,c) for(int a=b;a<=c;a++)
#define RT return 0;
#define LL long long
#define LXF int
#define RIN rin()
#define HH printf("\n")
#define All(x) (x).begin(),(x).end()
#define fi first
#define se second
using namespace std;
inline LXF rin(){
LXF x=0,w=1;
char ch=0;
while(ch<'0'||ch>'9'){
if(ch=='-') w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+(ch-'0');
ch=getchar();
}
return x*w;
}
priority_queue<pair<LL,int>,vector<pair<LL,int>>,greater<pair<LL,int>>> q;
LL dis[MAXN];
bool vis[MAXN];
int n,m;
class Work{
public:
int t,l,r;
LL c;
}a[MAXN];
class SegTree{
public:
int l,r;
bool _sort;
// set<pair<LL,int>> ls;
vector<pair<LL,int>> ls;
}tr1[MAXN<<2],tr2[MAXN<<2];
inline int lc(int x){return x<<1;}
inline int rc(int x){return x<<1|1;}
void Build(SegTree* tr,int p,int l,int r){
tr[p]._sort=0;
tr[p].l=l,tr[p].r=r;
if(l==r){
return ;
}
int mid=(l+r)>>1;
Build(tr,lc(p),l,mid);
Build(tr,rc(p),mid+1,r);
}
void Insert(SegTree* tr,int p,int pos,LL k){
// tr[p].ls.insert({k,pos});
tr[p].ls.push_back({k,pos});
if(tr[p].l==tr[p].r){
return ;
}
int mid=(tr[p].l+tr[p].r)>>1;
if(pos<=mid) Insert(tr,lc(p),pos,k);
else Insert(tr,rc(p),pos,k);
}
void Update(SegTree* tr,int p,int l,int r,LL top,int u){
if(l<=tr[p].l && tr[p].r<=r){
auto &ls=tr[p].ls;
if(!tr[p]._sort){
tr[p]._sort=1;
sort(All(ls),[&](pair<LL,int> p,pair<LL,int> q){
return p>q;
});
}
while(!ls.empty() && ls.back().fi<=top){
auto v=ls.back().se;
ls.pop_back();
if(dis[v]==LLONG_MAX){
dis[v]=dis[u]+a[v].c;
// cout<<u<<"->"<<v<<' '<<dis[u]<<' '<<dis[v]<<endl;
q.push({dis[v],v});
}
}
return ;
}
int mid=(tr[p].l+tr[p].r)>>1;
if(l<=mid) Update(tr,lc(p),l,r,top,u);
if(r>mid) Update(tr,rc(p),l,r,top,u);
}
signed main(){
n=RIN,m=RIN;
foru(i,1,m){
a[i]={RIN,RIN,RIN,RIN};
}
sort(a+1,a+1+m,[&](Work p,Work q){
return p.t<q.t;
});
Build(tr1,1,1,m);
Build(tr2,1,1,m);
foru(i,1,m){
Insert(tr1,1,i,a[i].t+a[i].l);
Insert(tr2,1,i,a[i].l-a[i].t);
}
foru(i,1,m){
if(a[i].l==1){
dis[i]=a[i].c;
q.push({dis[i],i});
}else{
dis[i]=LLONG_MAX;
}
}
while(!q.empty()){
auto [ds,u]=q.top();
q.pop();
if(vis[u]) continue;
vis[u]=1;
//do edge update
if(u>1){
Update(tr2,1,1,u-1,a[u].r-a[u].t+1,u);
}
if(u<m){
Update(tr1,1,u+1,m,a[u].r+a[u].t+1,u);
}
}
LL ans=LLONG_MAX;
foru(i,1,m){
if(a[i].r==n){
ans=min(ans,dis[i]);
// cout<<i<<' '<<dis[i]<<endl;
}
}
cout<<(ans==LLONG_MAX?-1:ans);
return 0;
}
T4
写完 T3 两个包还剩一个小时开 T4。赛后被同学提醒才想起来是今年省选 Day1 T1,我还没做出来,真事烂完了。题面不放了。
理解成了多个向量相加,移项什么的,变成一个点/圈同时移动/扩大,类似追及的一个问题,果断开分讨。讨论点所在的象限,查答案就行。写的时候写崩溃了,写成大粪了。
联合省选赛时的代码写的是二分,跑了 400+ms,90分因为开小了二分范围。这次赛后调过了,大分讨跑了 140ms,但是赛时获得 0 分高分,输完了。
//%^~
#include <bits/stdc++.h>
#define INF 0x7fffffff
#define MAXN 100005
#define eps 1e-9
#define foru(a,b,c) for(int a=b;a<=c;a++)
#define RT return 0;
#define LL long long
#define LXF int
#define RIN rin()
#define HH printf("\n")
#define All(x) (x).begin(),(x).end()
#define fi first
#define se second
using namespace std;
inline LXF rin(){
LXF x=0,w=1;
char ch=0;
while(ch<'0'||ch>'9'){
if(ch=='-') w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+(ch-'0');
ch=getchar();
}
return x*w;
}
LL n,k;
pair<LL,LL> ed,a[MAXN],pre[MAXN],dx;
LL get(pair<LL,LL> x){
return abs(x.fi)+abs(x.se);
}
pair<LL,LL> add(pair<LL,LL> p,pair<LL,LL> q){
return {p.fi+q.fi,p.se+q.se};
}
pair<LL,LL> mul(pair<LL,LL> p,LL q){
return {p.fi*q,p.se*q};
}
LL upc(LL a,LL b){
LL ret=a/b;
if(a!=ret*b) ret++;
return ret;
}
LL out(pair<LL,LL> A,pair<LL,LL> B,LL a,LL b){
if(get(A)<=k*a) return a;
LL v=get(add(A,B))-get(A);
// cout<<"!"<<k*b-v<<endl;
if(v<k*b){
return a+b*upc(get(A)-k*a,k*b-v);
}else{
// cout<<"sad"<<endl;
return LLONG_MAX;
}
}
LL oneout(pair<LL,LL> A,pair<LL,LL> B,LL a,LL b){
if(get(A)<=k*a) return a;
LL num=A.se/(-B.se);
if(get(add(A,mul(B,num)))<=k*(a+b*num)){
LL l=1,r=num,ans=0;
while(l<=r){
LL mid=(l+r)>>1ll;
if(get(add(A,mul(B,mid)))<=k*(a+b*mid)){
r=mid-1;
ans=mid;
}else{
l=mid+1;
}
}
return a+ans*b;
}else{
auto nA=add(A,mul(B,num+1));
auto nB=B;
nA.se=-nA.se;
nB.se=-nB.se;
return out(nA,nB,a+b*(num+1),n);
}
}
LL check(LL i){
auto A=pre[i];
auto B=dx;
if(pre[i].fi<0){
A.fi=-A.fi;
B.fi=-B.fi;
}
if(pre[i].se<0){
A.se=-A.se;
B.se=-B.se;
}
if(get(A)<=k*i) return i;
// if(get(add(A,B))<=k*(i+n)) return i+n;
LL ret=LLONG_MAX;
if(B.fi>=0 && B.se>=0){
// cout<<' '<<out(A,B,i,n)<<endl;
ret=min(ret,out(A,B,i,n));
}
if(B.fi<0 && B.se>=0){
swap(A.fi,A.se);
swap(B.fi,B.se);
}
if(B.fi>=0 && B.se<0){
// cout<<' '<<oneout(A,B,i,n)<<endl;
ret=min(ret,oneout(A,B,i,n));
// LL num=A.se/(-B.se);
// if(get(add(A,mul(B,num)))<=k*(i+n*num)){
// LL l=1,r=num,ans=0;
// while(l<=r){
// LL mid=(l+r)>>1ll;
// if(get(add(A,mul(B,mid)))<=k*(i+n*mid)){
// r=mid-1;
// ans=mid;
// }else{
// l=mid+1;
// }
// }
// ret=min(ret,ans);
// }else{
// auto nA=add(A,mul(B,num+1));
// auto nB=B;
// nA.se=-nA.se;
// nB.se=-nB.se;
// ret=min(ret,out(A,B,i+n*(num+1),n));
// }
}
if(B.fi<0 && B.se<0){
if(A.fi/(-B.fi)<A.se/(-B.se)){
swap(A.fi,A.se);
swap(B.fi,B.se);
}
LL num=A.se/(-B.se);
if(get(add(A,mul(B,num)))<=k*(i+n*num)){
LL l=1,r=num,ans=0;
while(l<=r){
LL mid=(l+r)>>1ll;
if(get(add(A,mul(B,mid)))<=k*(i+n*mid)){
r=mid-1;
ans=mid;
}else{
l=mid+1;
}
}
ret=min(ret,i+ans*n);
}else{
auto nA=add(A,mul(B,num+1));
auto nB=B;
if(nA.fi<=0 && nA.se<=0){
nA.fi=-nA.fi;
nA.se=-nA.se;
nB.fi=-nB.fi;
nB.se=-nB.se;
ret=min(ret,out(nA,nB,i+n*(num+1),n));
}else{
if(nA.fi>0 && nA.se<=0){
swap(nA.fi,nA.se);
swap(nB.fi,nB.se);
}
// cout<<nA.fi<<' '<<nA.se<<endl;
// cout<<nB.fi<<' '<<nB.se<<endl;
// if(get(add(nA,nB))<=k*(i+n)) return i+n;
nA.fi=-nA.fi;
nB.fi=-nB.fi;
// cout<<nA.fi<<' '<<nA.se<<endl;
// cout<<nB.fi<<' '<<nB.se<<endl;
// if(get(add(nA,nB))<=k*(i+n)) return i+n;
ret=min(ret,oneout(nA,nB,i+n*(num+1),n));
}
}
}
return ret;
}
signed main(){
int T=RIN;
while(T--){
n=RIN,k=RIN;
ed={RIN,RIN};
foru(i,1,n){
a[i]={RIN,RIN};
pre[i]=pre[i-1];
pre[i].fi+=a[i].fi;
pre[i].se+=a[i].se;
}
if(ed.fi==0 && ed.se==0){
cout<<0<<'\n';
continue;
}
dx.fi=-pre[n].fi;
dx.se=-pre[n].se;
foru(i,1,n){
pre[i].fi=ed.fi-pre[i].fi;
pre[i].se=ed.se-pre[i].se;
}
LL ans=LLONG_MAX;
foru(i,1,n){
ans=min(ans,check(i));
}
// if(ans==8){
// cout<<n<<' '<<k<<' '<<ed.fi<<' '<<ed.se<<endl;
// foru(i,1,n){
// cout<<a[i].fi<<' '<<a[i].se<<endl;
// }
// }
if(ans==LLONG_MAX){
cout<<(-1)<<'\n';
}else{
cout<<ans<<'\n';
}
}
return 0;
}