17-10-15模拟赛
T1:
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define inf 0x7fffffff 5 #define MN 2005 6 using namespace std; 7 inline int in(){ 8 int x=0;bool f=0; char c; 9 for (;(c=getchar())<'0'||c>'9';f=c=='-'); 10 for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); 11 return f?-x:x; 12 } 13 int f[MN],a[MN],h[MN],pos[MN]; 14 int n,cnt,res; 15 inline void dp(bool d){ 16 memset(f,0,sizeof(f));for (int i=1;i<=n;++i){ 17 if (d) for (int j=1;j<=cnt;++j) f[j]=min(f[j],(j-1)?f[j-1]:inf); 18 else for (int j=cnt;j;--j) f[j]=min(f[j],(j<cnt)?f[j+1]:inf); 19 for (int j=1;j<=cnt;++j) f[j]+=abs(a[pos[i]]-a[j]); 20 }for (int i=1;i<=cnt;++i) res=min(res,f[i]); 21 } 22 int main() 23 { 24 n=in();res=inf;for (int i=1;i<=n;++i) a[i]=h[i]=in(); 25 sort(a+1,a+n+1);cnt=unique(a+1,a+n+1)-a-1; 26 for (int i=1;i<=n;++i) pos[i]=lower_bound(a+1,a+n+1,h[i])-a; 27 dp(0);dp(1);printf("%d",res);return 0; 28 }
T2:
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define inf 0x3f3f3f3f 5 #define MM 100005 6 #define MN 105 7 using namespace std; 8 inline int in(){ 9 int x=0;bool f=0; char c; 10 for (;(c=getchar())<'0'||c>'9';f=c=='-'); 11 for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); 12 return f?-x:x; 13 } 14 int f[MM],p[MN],c[MN],res=inf,n,m; 15 int main() 16 { 17 n=in();m=in();memset(f,0x3f,sizeof(f));f[0]=0; 18 for (int i=1;i<=n;++i) p[i]=in(),c[i]=in(); 19 for (int i=1;i<=n;++i) 20 for (int j=p[i];j<=(m<<1);++j){ 21 if (j<m) f[j]=min(f[j],f[j-p[i]]+c[i]); 22 else res=min(res,f[j-p[i]]+c[i]); 23 }printf("%d",res);return 0; 24 }
T3:
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define MN 10005 5 using namespace std; 6 inline int in(){ 7 int x=0;bool f=0; char c; 8 for (;(c=getchar())<'0'||c>'9';f=c=='-'); 9 for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); 10 return f?-x:x; 11 } 12 struct rel{ 13 int a,b; 14 }p[MN]; 15 inline bool cmp(rel x,rel y){return (x.a==y.a)?x.b<y.b:x.a<y.a;} 16 inline bool comp(rel x,rel y){return (x.a==y.a)&&(x.b==y.b);} 17 int f[MN],n,l,h,m,sum=0,cnt=0; 18 int main() 19 { 20 n=in();l=in();h=in();m=in();f[1]+=h;f[n+1]-=h; 21 for (int i=1;i<=m;++i){ 22 p[i].a=in();p[i].b=in(); 23 if (p[i].a>p[i].b) swap(p[i].a,p[i].b); 24 }sort(p+1,p+m+1,cmp);cnt=unique(p+1,p+m+1,comp)-p-1; 25 for (int i=1;i<=cnt;++i) --f[p[i].a+1],++f[p[i].b]; 26 for (int i=1;i<=n;++i) sum+=f[i],printf("%d\n",sum);return 0; 27 }