noip2015普及組
阿新 • • 發佈:2017-08-13
gif 來看 優先隊列 style amp type 簡單 prior getch
看樣子2015的普及並不是很難
本人第四題做出來了,第三題居然不會做。。、、我果然弱(。??)ノ
後來看了一下題解恍然大悟、、
金幣——簡單模擬
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; inline int read(){ int t=1,num=0;char c=getchar(); while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();}View Codewhile(c>=‘0‘&&c<=‘9‘){num=num*10+c-‘0‘;c=getchar();} return num*t; } int main() { int n=1,k=read(),t=1,ans=0; while(n<=k){ ans+=t*t; t++;n+=t; } n=(t-n+k);ans+=t*n; printf("%d\n",ans); return 0; }
掃雷遊戲——簡單模擬
#include<iostream> #includeView Code<cstdio> #include<algorithm> #include<cstring> using namespace std; inline int read(){ int t=1,num=0;char c=getchar(); while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){num=num*10+c-‘0‘;c=getchar();} return num*t; } int n,m,a[111][111]; const int x[8]={-1,-1,-1,0,0,1,1,1}; const int y[8]={-1,0,1,-1,1,-1,0,1}; int main() { n=read();m=read(); for(int i=1;i<=n;i++){ char c[101];scanf("%s",c); for(int j=1;j<=m;j++){ if(c[j-1]==‘*‘)a[i][j]=-1; else a[i][j]=0; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i][j]==-1){printf("*");continue;} for(int k=0;k<8;k++){ int u=i+x[k],v=j+y[k]; if(u<1||v<1||u>n||v>m)continue; if(a[u][v]==-1)a[i][j]++; } printf("%d",a[i][j]); } puts(""); } return 0; }
求和——前綴和
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; inline int read(){ int t=1,num=0;char c=getchar(); while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){num=num*10+c-‘0‘;c=getchar();} return num*t; } const int maxn=100010,mod=10007; int n,m,a[maxn],c[maxn];long long ans=0; long long s[maxn][2],s1[maxn][2],s2[maxn][2],s3[maxn][2]; int main() { n=read();m=read(); for(int i=1;i<=n;i++)a[i]=read()%mod; for(int i=1;i<=n;i++)c[i]=read()%mod; for(int i=1;i<=n;i++){ int j=c[i],t=i&1; ans=(ans+s[j][t]*a[i]+((s1[j][t]*i)%mod)*a[i]+s2[j][t]+i*s3[j][t])%mod; s[j][t]=(s[j][t]+i)%mod; s1[j][t]=(s1[j][t]+1)%mod; s2[j][t]=(s2[j][t]+i*a[i])%mod; s3[j][t]=(s3[j][t]+a[i])%mod; } printf("%d\n",ans); return 0; }View Code
推銷員——優先隊列
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using namespace std; inline int read(){ int t=1,num=0;char c=getchar(); while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){num=num*10+c-‘0‘;c=getchar();} return num*t; } struct hehe{int a,b;}h[100010]; int n,now,ans=0,tmp=1,used[100010]; bool cmp(hehe a,hehe b){return a.a<b.a;} typedef pair<int,int> P; priority_queue<int> q1; struct luo{ int id,a,c; friend bool operator <(const luo x,const luo y){ return x.c<y.c; } }; priority_queue<luo> q2; int main() { n=read(); memset(used,0,sizeof(0)); for(int i=1;i<=n;i++)h[i].a=read(); for(int i=1;i<=n;i++)h[i].b=read(); sort(h+1,h+1+n,cmp); int la; for(int i=1;i<=n;i++) if(h[i].a*2+h[i].b>ans){ ans=h[i].a*2+h[i].b;now=h[i].a;la=i; } used[la]=1; for(int i=1;i<=n;i++){ if(h[i].a<=now)continue; q2.push((luo){i,h[i].a,h[i].a*2+h[i].b}); } printf("%d\n",ans); for(int i=2;i<=n;i++){ while(h[tmp].a<=now&&tmp<=n){ if(!used[tmp])q1.push(h[tmp].b); tmp++; } while(!q2.empty()||(q1.empty()&&q2.top().a<=now))q2.pop(); if(!q2.empty()&&q2.top().c-2*now>q1.top()){ ans+=q2.top().c-2*now; now=q2.top().a; used[q2.top().id]=1; q2.pop(); } else{ans+=q1.top();q1.pop();} printf("%d\n",ans); } return 0; }View Code
本文由Yzyet編寫,網址為www.cnblogs.com/Yzyet。非Yzyet同意,禁止轉載,侵權者必究。
noip2015普及組