UCF Local Programming Contest 2017 题解 A~I
A题
这题没啥好说的
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=1e6+10;
int main() {
LL a,b;
cin>>a>>b;
LL n;
cin>>n;
while (n--) {
LL c;
cin>>c;
cout<<c<<" ";
LL ans=0;
if (c>1000) {
ans+=1000*a+(c-1000)*b;
}
else {
ans=c*a;
}
cout<<ans<<endl;
}
return 0;
}
B题
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=1e6+10;
char g[3][9]={
'a','b','c','d','e','f','g','h','i',
'j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z','#',
};
int d[8][2]={1,0,-1,0,0,1,0,-1, 1,1,-1,-1,1,-1,-1,1};
void getXY(char c,int& x,int& y) {
int n=c-'a';
x=n/9;
y=n%9;
}
bool isSimi(string& a,string& b) {
bool f1=true;
for (int i=0;i<a.length();i++) {
if (a[i] == b[i]) continue;
int x,y;
getXY(a[i],x,y);
// cout<<x<< " "<< y<<endl;
bool f2=false;
for (int j=0;j<8;j++) {
int dx=d[j][0]+x, dy=d[j][1]+y;
if (dx>=0&&dy>=0&&dx<3&&dy<9) {
// cout<<g[dx][dy]<< " " <<endl;
if (g[dx][dy]==b[i]) {
f2=true;
break;
}
}
}
if (!f2) return false;
}
return true;
}
int main() {
string a,b;
int n;
cin>>n;
while (n--) {
cin>>a>>b;
if (a.length()!=b.length()) {
cout<<3<<endl;
}
else {
bool flag = true;
if (a==b) {
cout<<1<<endl;
}
else {
if (isSimi(a,b)) {
cout<<2<<endl;
}
else {
cout<<3<<endl;
}
}
}
}
return 0;
}
C题
就是比较比较左,比较比较右,基础题。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=1e6+10;
int main() {
LL t;
cin>>t;
while (t--) {
LL n,s;
cin>>n>>s;
LL ans=0,c,last;
for (int i=0;i<s;i++) {
cin>>c;
if (i!=0) {
if (last == c) {
ans++;
}
else if (last < c) {
LL rd=c-last-1;
LL ld=n-c+last+1;
ans+=min(ld,rd);
}
else {
LL ld=last-c+1;
LL rd=n-last+c-1;
ans+=min(ld,rd);
}
}
last=c;
}
cout<<ans<<endl;
}
return 0;
}
D题
这题很明显就能看出来是一道递推的题,可以用dp做,但是我们还要确定方向,所以我们直接用bfs好了,向四个方向推,数据范围也不大。
走一步之后,要么同行,要么不同行,同行判边界,异行也是判断会不会超出下一行的边界。
之后判一下是否出边界了,然后bfs本身具有最短路性质,就可以啦。
#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
typedef long long LL;
const int MAXN=1e6+10;
int vis[125][85],s[125];
int T,sx,sy,ex,ey,n;
int d[4][2]={1,0,-1,0,0,1,0,-1};
struct Node
{
int x, y, step;
Node(int _x, int _y, int _s) : x(_x), y(_y), step(_s) {}
};
int bfs() {
vis[sx][sy]=1;
queue<Node> q;
q.push(Node(sx, sy, 0));
while (!q.empty()) {
Node f = q.front();
q.pop();
if (f.x == ex && f.y == ey) {
return f.step;
}
for (int i=0; i<4; i++) {
int dx=f.x+d[i][0];
int dy=f.y+d[i][1];
if (dx == f.x) {
if (dy == -1) {
dx--;
dy=s[dx];
}
else if (dy > s[dx]) {
dx++;
dy=0;
}
}
else {
if (dy > s[dx]) dy=s[dx];
}
if (dx <=0 || dy < 0 || dx > n || dy > s[dx] || vis[dx][dy]) continue;
// cout<<dx<<" "<<dy<<endl;
vis[dx][dy]=1;
q.push(Node(dx,dy,f.step+1));
}
}
return 0;
}
int main() {
// freopen("in.txt","r",stdin);
cin>>T;
while (T--) {
memset(vis, 0, sizeof(vis));
cin>>n;
for (int i=1; i<=n; i++) {
cin>>s[i];
}
cin>>sx>>sy>>ex>>ey;
cout<<bfs()<<endl;
}
return 0;
}
E题
这题我是用tan做的,所以要处理四个区域内的情况。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=1e6+10;
const double EPS=1e-5;
const double PI=acos(-1);
double dis(double x,double y) {
return sqrt(x*x+y*y);
}
int main() {
// freopen("in.txt","r",stdin);
int T,K,b,d,s,w;
cin>>T;
while (T--) {
cin>>w>>b>>d>>s;
cin>>K;
double x,y;
int sc=0;
while (K--) {
cin>>x>>y;
int flag=-1;
if (dis(x,y) > (double)s) {
continue;
}
else if (dis(x,y) > (double)d) {
flag=1;
}
else if (dis(x,y) > (double)b) {
flag=2;
}
else {
flag=3;
sc+=50;
continue;
}
double k=abs(y/x);
double ang=atan(k)/PI*180;
double avgAng=1.0*360/w;
if (x<0 && y>0) {
ang=180-ang;
}
else if (x<0 && y<0) {
ang=ang+180;
}
else if (x>0 && y<0) {
ang=180+(180-ang);
}
double sum=0;
int ts=1;
for (int i=1;i<=w;i++) {
sum+=avgAng;
if (sum>ang) {
ts=i;
break;
}
}
if (flag == 2) {
sc+= 2 * ts;
}
else {
sc+= ts;
}
}
cout<<sc<<endl;
}
return 0;
}
F题
最短路的变形题啦,本来我是想用bfs状态表示的,但是看了题解之后还是决定用最短路写,之前用bfs写炸了一次空间,后来发现之前记录状态不对,但是还是最短路香。
对于城市,用4种状态作为点,4种状态之间全部用转换交通方式的费用连接。不同城市之间相同状态相连。
#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
typedef long long LL;
const int MAXN=410*4;
LL ts[MAXN],d[MAXN],vis[MAXN];
vector<pair<LL,LL>> g[MAXN];
struct Node {
LL id, d;
Node(LL _i,LL _d):id(_i),d(_d) {}
bool operator < (const Node& A) const {
return d > A.d;
}
};
void dijkstra(int s,int e) {
memset(vis, 0 ,sizeof(vis));
memset(d, 0x3f, sizeof(d));
d[s]=0;
priority_queue<Node> pq;
pq.push(Node(s,0));
while (!pq.empty())
{
Node f=pq.top();
pq.pop();
if (vis[f.id]) continue;
vis[f.id]=1;
for (auto i: g[f.id]) {
LL v=i.first;
LL c=i.second;
if (f.d+c<d[v]&&!vis[v]) {
d[v]=f.d+c;
pq.push(Node(v,d[v]));
}
}
}
}
int main() {
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
int T,C,R,VAL;
string U,V,MO;
cin>>T;
while (T--) {
cin>>C;
LL id=1;
unordered_map<string,int> cache;
while (C--) {
cin>>U>>ts[id];
cache[U]=id;
id+=4;
}
for (int i=1;i<id;i+=4) {
for (int j=i;j<i+4;j++) {
for (int k=j+1;k<i+4;k++) {
g[j].push_back(mk(k,ts[i]));
g[k].push_back(mk(j,ts[i]));
}
}
}
cin>>R;
while (R--) {
cin>>U>>V>>MO>>VAL;
LL mo=0;
if (MO=="AIR") mo=0;
else if (MO=="RAIL") mo=1;
else if (MO=="SEA") mo=2;
else mo=3;
int u=cache[U]+mo;
int v=cache[V]+mo;
g[u].push_back(mk(v,VAL));
g[v].push_back(mk(u,VAL));
}
cin>>U>>V;
int des=cache[U];
int s=0,e=id;
for (int i=0;i<4;i++) g[s].push_back(mk(des+i,0));
des=cache[V];
for (int i=0;i<4;i++) g[des+i].push_back(mk(e,0));
dijkstra(s,e);
cout<<d[e]<<endl;
for (LL i=0;i<id;i++) g[i].clear();
}
return 0;
}
G题
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=1e6+10;
double f[2505][10005], P[52];
int c[52];
int main() {
// freopen("in.txt","r",stdin);
int T,G;
scanf("%d",&T);
while (T--) {
scanf("%d",&G);
for (int i=1;i<=G;i++) {
scanf("%d%lf",&c[i],&P[i]);
// printf("%d %f
",c[i],P[i]);
}
int N;
scanf("%d",&N);
vector<double> p;
p.push_back(0);
for (int i=1;i<=G;i++) {
for (int j=1;j<=c[i];j++) {
p.push_back(P[i]);
}
}
memset(f, 0, sizeof(f));
f[0][0]=1;
for (int i=1;i<p.size();i++) {
for (int j=1;j<=N;j++) {
f[i][j]+=f[i-1][j-1]*p[i];
f[i-1][j]+=f[i-1][j-1]*(1-p[i]);
}
}
double ans=0;
for (int i=1;i<=N;i++) {
ans+=f[p.size()-1][i];
}
printf("%.3f
",ans);
}
return 0;
}
H题
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=1e6+10;
int f[105][105][105],cnt[105][105],a[105];
int LIS(int l,int r) {
vector<int> vc;
for (int i=l;i<=r;i++) {
auto it=lower_bound(vc.begin(),vc.end(),a[i]);
if (it == vc.end()) {
vc.push_back(a[i]);
}
else {
int ind=it-vc.begin();
vc[ind]=a[i];
}
}
// cout<<"LIS"<<endl;
// for (auto x: vc) cout<<x<<" ";
// cout<<endl;
return vc.size();
}
int main() {
// freopen("in.txt","r",stdin);
int T,N;
cin>>T;
while (T--) {
cin>>N;
for (int i=1;i<=N;i++) {
cin>>a[i];
}
for (int i=1;i<=N;i++) {
for (int j=i;j<=N;j++) {
cnt[i][j]=LIS(i,j);
}
}
memset(f, 0, sizeof(f));
// for (int i=1;i<=N;i++) {
// f[1][i][i]=1;
// }
for (int i=1;i<=cnt[1][N];i++) {
for (int len=1;len<=N;len++) {
// cout<<"len:"<<len<<endl;
for (int l=1;l+len-1<=N;l++) {
int r=l+len-1;
// cout<< f[i][l][r]<<endl;
if (cnt[l][r]>=i) {
f[i][l][r]=cnt[l][r];
}
for (int k=l;k<r;k++) {
f[i][l][r]=max(f[i][l][r],f[i][l][k]+f[i][k+1][r]);
// cout<<"i" << i<<" "<< f[i][l][r] << " "<< f[i][l][k]<< " "<< f[i][k+1][r]<<endl;
}
}
}
}
for (int i=1;i<N;i++) {
cout<<f[i][1][N]<< " ";
}
cout<<f[N][1][N]<<endl;
}
return 0;
}
I题
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
LL sum[MAXN],pos[MAXN];
LL lowbit(LL x) {
return x&(-x);
}
void update(LL i,LL val) {
while (i<MAXN) {
sum[i]+=val;
i+=lowbit(i);
}
}
LL query(LL i) {
LL res=0;
while (i>0) {
res+=sum[i];
i-=lowbit(i);
}
return res;
}
int main() {
// freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while (T--) {
memset(sum,0,sizeof(sum));
int N;
scanf("%d",&N);
LL num;
for (int i=1;i<=N;i++) {
scanf("%lld",&num);
pos[num]=i;
update(i,num);
}
LL p=0,ans=0;
for (int i=1;i<=N;i++) {
LL c=pos[i];
if (c>p) {
LL lc=query(c-1)-query(p-1);
LL rc=query(N)-lc;
ans+=min(lc,rc);
}
else {
LL rc=query(p-1)-query(c-1);
LL lc=query(N)-rc;
ans+=min(lc,rc);
}
update(c,-i);
p=c+1;
}
printf("%lld
",ans);
}
return 0;
}