USACO 3.2

2019年8月11日 6点热度 0条评论 来源: ssl_xjq_逐风之刃

解题报告

洛谷 1134 阶乘问题

代码(暴力)

/* ID:lemondi1 LANG:C++ TASK:fact4 */
#include<cstdio>
#include<cstring>
int n;
long long ans;
int main()
{ 
	freopen("fact4.in","r",stdin);
	freopen("fact4.out","w",stdout);
    scanf("%d",&n);
    ans=1;
    for(int i=2;i<=n;i++)
    { 
        ans*=1ll*i;
        while(ans%10==0) ans/=10;
        ans%=100000000;
    }
    printf("%lld\n",ans%10);
    return 0;
}

洛谷 2727 01串

代码(动态规划)

/* ID:lemondi1 LANG:C++ TASK:kimbits */
#include <cstdio>
#define rr register
using namespace std;
typedef unsigned uit;
uit n,tot,m,dp[41][41];
signed main(){ 
	freopen("kimbits.in","r",stdin);
	freopen("kimbits.out","w",stdout);
	scanf("%u%u%u",&n,&tot,&m);
	for (rr int i=0;i<=n;++i) dp[i][0]=dp[0][i]=1;
	for (rr int i=1;i<=n;++i)
	for (rr int j=1;j<=n;++j)
	    dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
	for (rr int i=n-1;~i;--i)
	if (dp[i][tot]>=m) putchar(48);
		else putchar(49),m-=dp[i][tot],--tot;
	return !putchar(10);
}

洛谷 2728 纺车的轮子

代码(暴力)

/* ID:lemondi1 LANG:C++ TASK:spin */
#include <cstdio>
#include <cctype>
#include <cstring>
#define rr register
using namespace std;
int speed[5],cnt[5],a[5][10],d[5][10],v[360];
inline signed iut(){ 
	rr int ans=0; rr char c=getchar();
	while (!isdigit(c)) c=getchar();
	while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
	return ans;
}
signed main(){ 
	freopen("spin.in","r",stdin);
	freopen("spin.out","w",stdout);
	for (rr int i=0;i<5;++i){ 
		speed[i]=iut(),cnt[i]=iut();
		for (rr int j=0;j<cnt[i];++j)
			a[i][j]=iut(),d[i][j]=iut();
	}
	for (rr int i=0;i<360;++i){ 
		memset(v,0,sizeof(v));
		for (rr int j=0;j<5;++j)
		for (rr int k=0;k<cnt[j];++k)
		for (rr int p=0;p<=d[j][k];++p)
		    ++v[(a[j][k]+i*speed[j]+p)%360];
		for (rr int j=0;j<360;++j)
		if (v[j]>=5) return !printf("%d\n",i);
	}
	return !printf("none\n");
}

洛谷 2729 饲料调配

代码(暴力+柯西不等式)

/* ID:lemondi1 LANG:C++ TASK:ratios */
#include <cstdio>
#define rr register
using namespace std;
int w1[4],w2[4],w3[4];
inline signed sqr(int x){ return x*x;}
signed main(){ 
	freopen("ratios.in","r",stdin);
	freopen("ratios.out","w",stdout);
	for (rr int i=3;~i;--i) scanf("%d%d%d",&w1[i],&w2[i],&w3[i]);
	for (rr int i=0;i<=100;++i)
	for (rr int j=0;j<=100;++j)
	for (rr int k=0;k<=100;++k){ 
		if (!i&&!j&&!k) continue;
		rr int p0=w1[0]*i+w1[1]*j+w1[2]*k,p1=w2[0]*i+w2[1]*j+w2[2]*k,p2=w3[0]*i+w3[1]*j+w3[2]*k;
		if (p0<w1[3]||p1<w2[3]||p2<w3[3]) continue;
		if ((sqr(w1[3])+sqr(w2[3])+sqr(w3[3]))*(sqr(p0)+sqr(p1)+sqr(p2))==sqr(w1[3]*p0+w2[3]*p1+w3[3]*p2))
			return !printf("%d %d %d %d\n",k,j,i,w1[3]?p0/w1[3]:(w2[3]?p1/w2[3]:p2/w3[3]));
	}
	return !printf("NONE\n");
}

洛谷 2730 魔板

代码(广搜+哈希)

/* ID:lemondi1 LANG:C++ TASK:msquare */
#include <cstdio>
#include <cstring>
#define p 48383
using namespace std;
const char rules[3][8]={ { '8','7','6','5','4','3','2','1'},{ '4','1','2','3','6','7','8','5'},{ '1','7','2','4','5','3','6','8'}};
char a[9],answer[p+1],list[p+1][9],hash[p+1][9],s[p+1]; int ans,f[p+1];
bool check(char *s){ 
    int k=0,i=0;
    for (int i=1;i<=8;i++) k=k*10+s[i]-48;
    int orig=k%p;
    while (i<p&&!strstr(hash[(orig+i)%p]+1,s+1)&&hash[(orig+i)%p][1]!='\0') i++;
    if (strstr(hash[(orig+i)%p]+1,s+1)) return true; else strcpy(hash[(orig+i)%p]+1,s+1); return false;
}
void print(int tail){ 
    if (!tail) return;
    print(f[tail]);
    if (answer[tail]!='@') s[++ans]=answer[tail];
}
void bfs(){ 
    int head=0,tail=1; answer[1]=64;strcpy(list[1]+1,"12345678"); strcpy(hash[12345678%p]+1,list[1]+1);
    do{ 
        head++;
        for (int i=0;i<3;i++){ 
            f[++tail]=head;
            answer[tail]=i+65;
            strcpy(list[tail]+1,list[head]+1);
            for (int j=1;j<=8;j++) list[tail][j]=list[head][rules[i][j-1]-48];
            if (check(list[tail])) tail--;
            if (strstr(list[tail]+1,list[0]+1)){ print(tail);return;}
        }
    }while (head<tail);
}
int main(){ 
	freopen("msquare.in","r",stdin);
	freopen("msquare.out","w",stdout);
    for (int i=1;i<=8;i++) scanf("%d",&a[i]),a[i]+=48; strcpy(list[0]+1,a+1);
    if (strstr(list[0]+1,"12345678")) { putchar('0'),putchar(10),putchar(10); return 0;}
    bfs(); printf("%d\n",ans); puts(s+1);
    return 0;
}

洛谷 1828 香甜的黄油

代码(dijkstra+堆优化)

/* ID:lemondi1 LANG:C++ TASK:butter */
#include <cstdio>
#include <queue>
#include <algorithm>
struct node{ int y,w,next;}e[2901];
int n,m,dis[801][801],ls[801],mark[801];
struct rec{ 
	int x,d;
	bool operator <(const rec &t)const{ 
	    return d>t.d;
	}
};
std::priority_queue<rec>q; int ans=1000000;
inline int in(){ 
    int ans=0; char c=getchar();
    while (c<48||c>57) c=getchar();
    while (c>47&&c<58) ans=ans*10+c-48,c=getchar();
    return ans;
}
inline void print(int ans){ 
	if (ans>9) print(ans/10);
	putchar(ans%10+48);
}
inline int min(int a,int b){ 
	if (a<b) return a; else return b;
}
int main(){ 
	freopen("butter.in","r",stdin);
	freopen("butter.out","w",stdout); 
	int k=in(); n=in(); m=in();
	while (k--) mark[in()]++;
	for (register int i=1;i<=m;i++){ 
		register int x=in(),y=in(),w=in();
		e[i]=(node){ y,w,ls[x]}; ls[x]=i;
		e[i+m]=(node){ x,w,ls[y]}; ls[y]=i+m;
	}
	for (register int j=1;j<=n;j++){ 
        while (q.size()) q.pop();
	    for (register int i=1;i<=n;i++) dis[j][i]=1000000;
	    dis[j][j]=0; q.push((rec){ j,0});
	    while (q.size()){ 
		    register int x=q.top().x,d=q.top().d; q.pop();
		    if (dis[j][x]!=d) continue;
		    for (register int i=ls[x];i;i=e[i].next)
		    if (dis[j][x]+e[i].w<dis[j][e[i].y]){ 
			    dis[j][e[i].y]=dis[j][x]+e[i].w;
			    q.push((rec){ e[i].y,dis[j][e[i].y]});
		    }
	    }
	}
	for (register int i=1;i<=n;i++){ 
		register int sum=0;
		for (register int j=1;j<=n;j++)
		    sum+=dis[i][j]*mark[j];
		ans=min(sum,ans);
	}
	return !printf("%d\n",ans);
}
    原文作者:ssl_xjq_逐风之刃
    原文地址: https://blog.csdn.net/sugar_free_mint/article/details/99164373
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。