TJU[2022冬季训练]个人自测赛(二)(下)
问题F POJ 2976
题目中给出了两个数组,然后要求出来去掉k个之后最大的累计平均值
我们采取二分的方法,令题目中这个式子的其中(n-k)项大于等于x。
而判断n-k个数如何选择,自然选择排序的方法。
代码如下:(POJ炸了不确定对不对)
#include<bits/stdc++.h> #define re register #define ll long long #define inc(i,j,k) for(re int i=j;i<=k;i++) #define dec(i,j,k) for(re int i=j;i>=k;i--) using namespace std; inline ll read() { re ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9') {x=x*10+(ch^48); ch=getchar();} return x*f; } ll a[1010],b[1010]; ll n,k; double c[1010]; const double eps = 1e-20; bool check(double x) { inc(i,1,n) c[i]=a[i]*100-x*b[i]; sort(c+1,c+1+n); double res=0; inc(i,1,n-k) res+=c[n-i+1]; return res>=0; } inline void ef() { double l=0; double r=1000; inc(ttt,1,50) { double m=(r+l)/2; if(check(m)) l=m; else r=m; } printf("%.f\n",(l)); } int main() { while(1) { n=read(); k=read(); if(!n && !k) break; inc(i,1,n) a[i]=read(); inc(i,1,n) b[i]=read(); ef(); } }
问题H 黑暗爆炸1085
一眼搜索,,但是普通的搜索肯定是不行的,需要剪枝,(为什么我是先想到的剪枝再想到的算法),很简单的IDA*,估值函数就是距离目标还差多少颗棋子,因为每次转移状态也只能改变两个位置的状态,自己的地图和标准地图差太多是肯定走不到的,于是剪枝就好了
#include<bits/stdc++.h> #define re register #define ll long long #define inc(i,j,k) for(re int i=j;i<=k;i++) #define dec(i,j,k) for(re int i=j;i>=k;i--) using namespace std; inline int read() { re int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9') {x=x*10+(ch^48); ch=getchar();} return x*f; } const int st[7][7]={ {0,0,0,0,0,0}, {0,1,1,1,1,1}, {0,0,1,1,1,1}, {0,0,0,2,1,1}, {0,0,0,0,0,1}, {0,0,0,0,0,0} }; int T; bool success; int mp[7][7]; const int dx[]={0,1,1,-1,-1,2,2,-2,-2}; const int dy[]={0,2,-2,2,-2,1,-1,1,-1}; inline bool check() { inc(i,1,5) { inc(j,1,5) { if(mp[i][j]!=st[i][j]) { return false; } } } return true; } int sxx,syy; inline int Astar() { int res=0; inc(i,1,5) { inc(j,1,5) { if(mp[i][j]!=st[i][j]) { res++; } } } return res; } inline void dfs(int dep,int x,int y,int q) { if(dep==q) { if(check()) success=1; return; } inc(i,1,8) { int nx=x+dx[i]; int ny=y+dy[i]; if(nx<1||nx>5||ny<1||ny>5) continue; swap(mp[nx][ny],mp[x][y]); int tmp=Astar(); if(tmp+dep<=q) dfs(dep+1,nx,ny,q); swap(mp[nx][ny],mp[x][y]); } } int main() { T=read(); char ch; inc(Zzt,1,T) { success=0; inc(i,1,5) { inc(j,1,5) { cin>>ch; if(ch=='*') { mp[i][j]=2; sxx=i; syy=j; } else mp[i][j]=ch-'0'; } } if(!Astar()) { cout<<0<<endl; continue; } inc(maxdep,1,15) { dfs(0,sxx,syy,maxdep); if(success) { cout<<maxdep<<endl; break; } } if(!success) cout<<-1<<endl; } } /* 2 10110 01*11 10111 01001 00000 01011 110*1 01110 01010 00100 */
我是真服了,,,tnnd,最后因为一个cin和getchar调了半天,,,,,把getchar换成cin就过了。。。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了