I wrote it until now, but I still haven't seen it in the middle ..
A: Aizu 0009 prime number: Prime Number filtering. Note that the memory may burst !!.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int N=1e7;int vis[N];void initPrime(){ int num = 0, m = sqrt (N + 0.5); REPF(i,1,N) vis[i]=1; for (int i = 2; i <= m; ++i) if (vis[i] == 1) for (int j = i * i; j <= N; j += i) vis[j] = 0; vis[1]=0; for(int i=2;i<=N;i++) vis[i]+=vis[i-1];// for(int i=2;i<=10;i++)// cout<<"233 "<<sum[i]<<endl;}int main(){ int n; initPrime(); while(~scanf("%d",&n)) printf("%d\n",vis[n]); return 0;}
B: Aizu 2224 save your cat...
C: Cf 250a paper work
The number of negative numbers in each consecutive segment cannot exceed 2. Messing around.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )int a[110],n;int num[110];int main(){ while(~scanf("%d",&n)) { REPF(i,1,n) scanf("%d",&a[i]); CLEAR(num,0); int cnt=0; int l=0; int m=0; REPF(i,1,n) { m++; if(a[i]<0) cnt++; if(cnt==2) { cnt=0;int j; for(j=i+1;j<=n;j++) { if(a[j]<0) break; else m++; } num[l++]=m; i=j-1; m=0; } if(i==n&&cnt==1) num[l++]=m; } if(l==0) { printf("%d\n%d\n",1,n); continue; } printf("%d\n",l); REP(i,l) printf(i==l-1?"%d\n":"%d ",num[i]); }}
D: Cf 126B password:
The longest string is its prefix and suffix, which is the same as a string in the middle.
KMP practices:
# Include <iostream> # include <cstdio> # include <cstring> # define LMT 1000005 using namespace STD; int hash [LMT], next [LMT], Len; char STR [LMT]; void Init (void) {int I = 0, j =-1; next [0] =-1; while (I <Len) {If (j =-1 | STR [I] = STR [J]) {I ++; j ++; next [I] = J ;} else J = next [J];} for (I = 0; I <Len; I ++) {// cout <"2333" <next [I] <Endl; hash [next [I] ++ ;}} int main () {int I; scanf ("% s", STR); Len = strlen (STR); Init (); I = Len; while (next [I]> 0) // corresponds to AAA. In this case, {If (hash [next [I]) {for (Int J = 0; j <next [I]; j ++) printf ("% C", STR [J]); printf ("\ n"); Return 0;} I = next [I];} printf ("just a legend \ n"); Return 0 ;}
F: Cf 303 D biridian Forest
Question: When the maze reaches the exit, there is a duel in the middle. Ask for the minimum number of fights. Reverse BFs.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=1100;char mp[maxn][maxn];int val[maxn][maxn];int dis[maxn][maxn];int vis[maxn][maxn];int dr[4][2]={{-1,0},{1,0},{0,-1},{0,1}};int n,m,ex,ey;void BFS(){ pair<int,int>st; CLEAR(vis,0); vis[ex][ey]=1; dis[ex][ey]=1; queue<pair<int,int> >q; int dd=0x7fffff; int num=0; q.push(make_pair(ex,ey)); while(!q.empty()) { st=q.front(); q.pop(); if(dis[st.first][st.second]>dd) break; REP(i,4) { int xx=st.first+dr[i][0]; int yy=st.second+dr[i][1]; if(mp[xx][yy]!='T'&&!vis[xx][yy]&&xx>=0&&xx<n&&yy>=0&&yy<m) { dis[xx][yy]=dis[st.first][st.second]+1; vis[xx][yy]=1; if(dis[xx][yy]<=dd) num+=val[xx][yy]; if(mp[xx][yy]=='S') dd=dis[xx][yy]; q.push(make_pair(xx,yy)); } } } printf("%d\n",num);}int main(){ while(~scanf("%d%d",&n,&m)) { REP(i,n) scanf("%s",mp[i]); CLEAR(val,0); REP(i,n) { REP(j,m) { if(mp[i][j]=='E') { ex=i; ey=j; } if(mp[i][j]>='0'&&mp[i][j]<='9') val[i][j]=mp[i][j]-'0'; } } BFS(); } return 0;}
G CF 215 D hot days.
Long long is infinite in the middle, and the extreme values are obtained at both ends.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=1e5+100;LL t[maxn],T[maxn],x[maxn],cost[maxn];int n,m;LL solve(int f,LL xx){ int l=1,r=m/xx+(m%xx?1:0); LL maxnn; if(m<=xx) maxnn=cost[f]; else maxnn=cost[f]+x[f]*m; if((LL)r*xx<m) maxnn=min(maxnn,(LL)r*cost[f]+((LL)(m-xx*r)+xx)*x[f]); else maxnn=min(maxnn,(LL)r*cost[f]); return maxnn;}int main(){ while(~scanf("%d%d",&n,&m)) { LL sum=0; REPF(i,1,n) scanf("%I64d%I64d%I64d%I64d",&t[i],&T[i],&x[i],&cost[i]); REPF(i,1,n) { if(T[i]-t[i]>0) { LL xx=T[i]-t[i]; sum+=solve(i,xx); } else sum+=((LL)m*x[i]+cost[i]); } printf("%I64d\n",sum); } return 0;}
I: HDU 1353/poj 2581 is a violent water problem. I have been thinking about how to record the path. In fact, I started to think of violence and did not dare to write it. C ++ is required for poj.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )int main(){ int num1,num2,num3,num4; int a,b,c,d;double x; while(~scanf("%lf%d%d%d%d",&x,&num1,&num2,&num3,&num4)) { int n=(int)(x*100);// cout<<n<<endl; int flag=0; for(int i=0;i<=num4;i++) { for(int j=0;j<=num3;j++) { for(int k=0;k<=num2;k++) { for(int l=0;l<=num1;l++) { if(i+5*j+10*k+25*l==n) { a=l;b=k;c=j;d=i; flag=1; break; } } if(flag) break; } if(flag) break; } if(flag) break; } if(flag) printf("%d %d %d %d\n",a,b,c,d); else printf("NO EXACT CHANGE\n"); } return 0;}
J: HDU 1595 find the longest of the shortest
Dijkstra calculates the maximum value of the split edge record after record the precursor.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=1100;int mp[maxn][maxn];int vis[maxn],dis[maxn];int pre[maxn];int n,m;void dijkstra(int x){ int pos; CLEAR(vis,0); REPF(i,1,n) dis[i]=mp[1][n]; dis[1]=0;// vis[1]=1; REPF(i,1,n) { pos=-1; REPF(j,1,n) { if(!vis[j]&&(pos==-1||dis[pos]>dis[j])) pos=j; } vis[pos]=1; REPF(j,1,n) { if(!vis[j]&&dis[j]>dis[pos]+mp[pos][j]) { dis[j]=dis[pos]+mp[pos][j]; if(x) pre[j]=pos; } } }}int main(){ int u,v,w; while(~scanf("%d%d",&n,&m)) { CLEAR(mp,0x3f3f3f);// REPF(i,1,n) mp[i][i]=0; CLEAR(pre,-1); while(m--) { scanf("%d%d%d",&u,&v,&w); if(mp[u][v]>w) mp[u][v]=mp[v][u]=w;// cout<<mp[u][v]<<endl; } dijkstra(1);// cout<<"2333 "<<dis[n]<<endl; int dd=dis[n]; for(int i=n;i!=1;i=pre[i]) { int t=mp[i][pre[i]];// cout<<"666 "<<endl; mp[i][pre[i]]=mp[pre[i]][i]=0x3f3f3f; dijkstra(0); if(dis[n]>dd) dd=dis[n]; mp[i][pre[i]]=mp[pre[i]][i]=t; } printf("%d\n",dd); } return 0;}
Week SP1: 2014/11/2