delete pics to save space
This commit is contained in:
32
history_source/挑战程序设计竞赛/1.6.1.cpp
Normal file
32
history_source/挑战程序设计竞赛/1.6.1.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include<cstdio>
|
||||
const int MAXN=105;
|
||||
int n,a[MAXN],ans;
|
||||
inline int MAX(int a,int b)
|
||||
{return a>b?a:b;}
|
||||
int max(int a,int b,int c)
|
||||
{return MAX(a,MAX(b,c));}
|
||||
void solve()
|
||||
{
|
||||
int ans=0;
|
||||
for(int i=0;i<n;i++)
|
||||
for(int j=i+1;j<n;j++)
|
||||
for(int k=j+1;k<n;k++)
|
||||
{
|
||||
int len=a[i]+a[j]+a[k];
|
||||
int ma=max(a[i],a[j],a[k]);
|
||||
int rest=len-ma;
|
||||
if(ma<rest) ans=MAX(ans,len);
|
||||
}
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++) scanf("%d",&a[i]);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
28
history_source/挑战程序设计竞赛/1.6.2.cpp
Normal file
28
history_source/挑战程序设计竞赛/1.6.2.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int L,n,x[1000005];
|
||||
void solve()
|
||||
{
|
||||
int minT=0;
|
||||
for(int i=0;i<n;i++) minT=max(minT,min(x[i],L-x[i]));
|
||||
int maxT=0;
|
||||
for(int i=0;i<n;i++) maxT=max(maxT,max(x[i],L-x[i]));
|
||||
printf("%d %d\n",minT,maxT);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&L,&n);
|
||||
for(int i=0;i<n;i++) scanf("%d",x+i);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
27
history_source/挑战程序设计竞赛/2.1.4.1.cpp
Normal file
27
history_source/挑战程序设计竞赛/2.1.4.1.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include<cstdio>
|
||||
int a[22],n,k;
|
||||
bool dfs(int i,int sum)
|
||||
{
|
||||
if(i==n) return sum==k;
|
||||
if(dfs(i+1,sum)) return 1;
|
||||
if(dfs(i+1,sum+a[i])) return 1;
|
||||
return 0;
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int i;
|
||||
scanf("%d",&n);
|
||||
for(i=0;i<n;i++) scanf("%d",a+i);
|
||||
scanf("%d",&k);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
if(dfs(0,0)) printf("Yes\n");
|
||||
else printf("No\n");
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
41
history_source/挑战程序设计竞赛/2.1.4.2.cpp
Normal file
41
history_source/挑战程序设计竞赛/2.1.4.2.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include<cstdio>
|
||||
int xx,yy;
|
||||
char field[105][105];
|
||||
void dfs(int x,int y)
|
||||
{
|
||||
field[x][y]='.';
|
||||
int dx,dy,nx,ny;
|
||||
for(dx=-1;dx<=1;dx++)
|
||||
for(dy=-1;dy<=1;dy++)
|
||||
{
|
||||
nx=x+dx,ny=y+dy;
|
||||
if(0<=nx&&nx<xx&&0<=ny&&ny<yy&&field[nx][ny]=='W') dfs(nx,ny);
|
||||
}
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int res=0,i,j;
|
||||
for(i=0;i<yy;i++)
|
||||
for(j=0;j<xx;j++)
|
||||
if(field[j][i]=='W')
|
||||
{
|
||||
dfs(j,i);
|
||||
res++;
|
||||
}
|
||||
printf("%d\n",res);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&yy,&xx);
|
||||
for(int i=0;i<yy;i++)
|
||||
{
|
||||
getchar();
|
||||
for(int j=0;j<xx;j++) field[j][i]=getchar();
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
56
history_source/挑战程序设计竞赛/2.1.5.1.cpp
Normal file
56
history_source/挑战程序设计竞赛/2.1.5.1.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#include<cstdio>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int x,y;
|
||||
node(int xx,int yy){x=xx,y=yy;}
|
||||
node(){}
|
||||
};
|
||||
char map[105][105];
|
||||
int xx,yy,sx,sy,ex,ey,dis[105][105],dx[]={0,0,-1,1},dy[]={-1,1,0,0};
|
||||
inline int min(int a,int b){return a<b?a:b;}
|
||||
int bfs()
|
||||
{
|
||||
queue<node>que;
|
||||
node p;
|
||||
que.push(node(sx,sy));
|
||||
for(int i=0;i<yy;i++)
|
||||
for(int j=0;j<xx;j++) dis[j][i]=100000000;
|
||||
dis[sx][sy]=0;
|
||||
while(que.size()>0)
|
||||
{
|
||||
p=que.front();que.pop();
|
||||
if(p.x==ex&&p.y==ey) return dis[ex][ey];
|
||||
for(int i=0;i<4;i++)
|
||||
if(p.x+dx[i]>=0&&p.x+dx[i]<xx&&p.y+dy[i]>=0&&p.y+dy[i]<yy&&map[p.x+dx[i]][p.y+dy[i]]!='#'&&dis[p.x+dx[i]][p.y+dy[i]]==100000000)
|
||||
{
|
||||
que.push(node(p.x+dx[i],p.y+dy[i]));
|
||||
dis[p.x+dx[i]][p.y+dy[i]]=dis[p.x][p.y]+1;
|
||||
}
|
||||
}
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
printf("%d\n",bfs());
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&xx,&yy);
|
||||
for(int i=0;i<yy;i++)
|
||||
{
|
||||
getchar();
|
||||
for(int j=0;j<xx;j++)
|
||||
{
|
||||
map[j][i]=getchar();
|
||||
if(map[j][i]=='S') sx=j,sy=i;
|
||||
if(map[j][i]=='G') ex=j,ey=i;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
26
history_source/挑战程序设计竞赛/2.2.1.cpp
Normal file
26
history_source/挑战程序设计竞赛/2.2.1.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include<cstdio>
|
||||
const int V[6]={1,5,10,50,100,500};
|
||||
int C[6],A;
|
||||
inline int min(int a,int b){return a<b?a:b;}
|
||||
void solve()
|
||||
{
|
||||
int ans=0;
|
||||
for(int i=5;i>=0;i--)
|
||||
{
|
||||
int t=min(A/V[i],C[i]);
|
||||
A-=t*V[i];
|
||||
ans+=t;
|
||||
}
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
for(int i=0;i<6;i++) scanf("%d",C+i);
|
||||
scanf("%d",&A);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
36
history_source/挑战程序设计竞赛/2.2.2.cpp
Normal file
36
history_source/挑战程序设计竞赛/2.2.2.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int MAX_N=100000;
|
||||
int n,s[MAX_N],t[MAX_N];
|
||||
pair<int,int> itv[MAX_N];
|
||||
void solve()
|
||||
{
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
itv[i].first=t[i];
|
||||
itv[i].second=s[i];
|
||||
}
|
||||
sort(itv,itv+n);
|
||||
int ans=0,t=0;
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
if(t<itv[i].second)
|
||||
{
|
||||
ans++;
|
||||
t=itv[i].first;
|
||||
}
|
||||
}
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++) scanf("%d",s+i);
|
||||
for(int i=0;i<n;i++) scanf("%d",t+i);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
43
history_source/挑战程序设计竞赛/2.2.3.cpp
Normal file
43
history_source/挑战程序设计竞赛/2.2.3.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include<cstdio>
|
||||
int n;
|
||||
char s[2005];
|
||||
void solve()
|
||||
{
|
||||
int a=0,b=n-1,sum=0;
|
||||
while(a<=b)
|
||||
{
|
||||
bool left=0;
|
||||
for(int i=0;a+i<=b;i++)
|
||||
{
|
||||
if(s[a+i]<s[b-i])
|
||||
{
|
||||
left=1;
|
||||
break;
|
||||
}
|
||||
else if(s[a+i]>s[b-i])
|
||||
{
|
||||
left=0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(left) putchar(s[a++]);
|
||||
else putchar(s[b--]);
|
||||
sum++;
|
||||
if(sum%80==0) putchar('\n');
|
||||
}
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
getchar();
|
||||
s[i]=getchar();
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
35
history_source/挑战程序设计竞赛/2.2.4.1.cpp
Normal file
35
history_source/挑战程序设计竞赛/2.2.4.1.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int r,n,x[1005];
|
||||
void solve()
|
||||
{
|
||||
sort(x,x+n);
|
||||
static int i,ans,s,p;
|
||||
i=ans=0;
|
||||
while(i<n)
|
||||
{
|
||||
s=x[i++];
|
||||
while(i<n&&x[i]<=s+r) i++;
|
||||
p=x[i-1];
|
||||
while(i<n&&x[i]<=p+r) i++;
|
||||
ans++;
|
||||
}
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&r,&n);
|
||||
if(r==-1&&n==-1) exit(0);
|
||||
static int i;
|
||||
for(i=0;i<n;i++) scanf("%d",x+i);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
84
history_source/挑战程序设计竞赛/2.2.4.2.cpp
Normal file
84
history_source/挑战程序设计竞赛/2.2.4.2.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
#include<cstdio>
|
||||
#include<vector>
|
||||
struct heap
|
||||
{
|
||||
int SIZE;
|
||||
std::vector<int> v;
|
||||
heap()
|
||||
{
|
||||
SIZE=0;
|
||||
}
|
||||
int push(int vv)
|
||||
{
|
||||
static int p,i;
|
||||
i=SIZE++;
|
||||
if(i>=v.size()) v.push_back(int());
|
||||
while(i>0)
|
||||
{
|
||||
p=(i-1)/2;
|
||||
if(v[p]<=vv) break;
|
||||
v[i]=v[p];
|
||||
i=p;
|
||||
}
|
||||
v[i]=vv;
|
||||
return vv;
|
||||
}
|
||||
int top()
|
||||
{
|
||||
if(SIZE>0) return v[0];
|
||||
return int();
|
||||
}
|
||||
int pop()
|
||||
{
|
||||
static int i,a,b;
|
||||
static int x,ret;
|
||||
ret=v[0];
|
||||
x=v[--SIZE];
|
||||
i=0;
|
||||
while(i*2+1<SIZE)
|
||||
{
|
||||
a=i*2+1,b=i*2+2;
|
||||
if(b<SIZE&&v[b]<v[a]) a=b;
|
||||
if(v[a]>=x) break;
|
||||
v[i]=v[a];
|
||||
i=a;
|
||||
}
|
||||
v[i]=x;
|
||||
return ret;
|
||||
}
|
||||
inline int size()
|
||||
{
|
||||
return SIZE;
|
||||
}
|
||||
};
|
||||
heap hp;
|
||||
void solve()
|
||||
{
|
||||
long long a,b,ans=0;
|
||||
while(hp.size()>1)
|
||||
{
|
||||
a=hp.top();
|
||||
hp.pop();
|
||||
b=hp.top();
|
||||
hp.pop();
|
||||
ans+=a+b;
|
||||
hp.push(a+b);
|
||||
}
|
||||
printf("%lld\n",ans);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int n,t;
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
scanf("%d",&t);
|
||||
hp.push(t);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
26
history_source/挑战程序设计竞赛/2.3.1.1.cpp
Normal file
26
history_source/挑战程序设计竞赛/2.3.1.1.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include<cstdio>
|
||||
const int MAXN=105;
|
||||
int n,W;
|
||||
int w[MAXN],v[MAXN];
|
||||
int dp[MAXN];
|
||||
inline int max(int a,int b){return a>b?a:b;}
|
||||
void solve()
|
||||
{
|
||||
int i,j;
|
||||
for(i=0;i<n;i++)
|
||||
for(j=W;j>=w[i];j--)
|
||||
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
|
||||
printf("%d\n",dp[W]);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++) scanf("%d%d",w+i,v+i);
|
||||
scanf("%d",&W);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
27
history_source/挑战程序设计竞赛/2.3.1.2.cpp
Normal file
27
history_source/挑战程序设计竞赛/2.3.1.2.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
string s1,s2;
|
||||
int dp[1001][1001];
|
||||
inline int max(int a,int b){return a>b?a:b;}
|
||||
void solve()
|
||||
{
|
||||
int i,j;
|
||||
for(i=1;i<s1.size();i++)
|
||||
for(j=1;j<s2.size();j++)
|
||||
if(s1[i]==s2[j]) dp[i][j]=dp[i-1][j-1]+1;
|
||||
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
|
||||
printf("%d\n",dp[s1.size()-1][s2.size()-1]);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
cin>>s1>>s2;
|
||||
s1=" "+s1;
|
||||
s2=" "+s2;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
24
history_source/挑战程序设计竞赛/2.3.2.1.cpp
Normal file
24
history_source/挑战程序设计竞赛/2.3.2.1.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include<cstdio>
|
||||
int n,w[105],v[105],W;
|
||||
int dp[10005];
|
||||
inline int max(int a,int b){return a>b?a:b;}
|
||||
void solve()
|
||||
{
|
||||
int i,j;
|
||||
for(i=0;i<n;i++)
|
||||
for(j=w[i];j<=W;j++)
|
||||
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
|
||||
printf("%d\n",dp[W]);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++) scanf("%d%d",w+i,v+i);
|
||||
scanf("%d",&W);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
32
history_source/挑战程序设计竞赛/2.3.2.2.cpp
Normal file
32
history_source/挑战程序设计竞赛/2.3.2.2.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int MAXN=100,MAXV=100;
|
||||
int n,W;
|
||||
int w[MAXN+1],v[MAXN+1];
|
||||
int dp[MAXN+1][MAXN*MAXV+1];
|
||||
inline int max(int a,int b){return a>b?a:b;}
|
||||
void solve()
|
||||
{
|
||||
fill(dp[0],dp[0]+MAXN*MAXV+1,1<<30);
|
||||
dp[0][0]=0;
|
||||
for(int i=0;i<n;i++)
|
||||
for(int j=0;j<=MAXN*MAXV;j++)
|
||||
if(j<v[i]) dp[i+1][j]=dp[i][j];
|
||||
else dp[i+1][j]=min(dp[i][j],dp[i][j-v[i]]+w[i]);
|
||||
int res=0;
|
||||
for(int i=0;i<=MAXN*MAXV;i++) if(dp[n][i]<=W) res=i;
|
||||
printf("%d\n",res);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++) scanf("%d%d",w+i,v+i);
|
||||
scanf("%d",&W);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
29
history_source/挑战程序设计竞赛/2.3.2.3.cpp
Normal file
29
history_source/挑战程序设计竞赛/2.3.2.3.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
int n,k,a[105],m[105];
|
||||
int dp[100005];
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++) scanf("%d",a+i);
|
||||
for(int i=0;i<n;i++) scanf("%d",m+i);
|
||||
scanf("%d",&k);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
memset(dp,-1,sizeof(dp));
|
||||
dp[0]=0;
|
||||
for(int i=0;i<n;i++)
|
||||
for(int j=0;j<=k;j++)
|
||||
if(dp[j]>=0) dp[j]=m[i];
|
||||
else if(j<a[i]||dp[j-a[i]]<=0) dp[j]=-1;
|
||||
else dp[j]=dp[j-a[i]]-1;
|
||||
if(dp[k]>=0) printf("Yes\n");
|
||||
else printf("No\n");
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
27
history_source/挑战程序设计竞赛/2.3.2.4.cpp
Normal file
27
history_source/挑战程序设计竞赛/2.3.2.4.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
#define INF ((int)1<<31-1)
|
||||
using namespace std;
|
||||
int n,a[1005],dp[1005];
|
||||
void solve()
|
||||
{
|
||||
fill(dp,dp+n,INF);
|
||||
for(int i=1;i<n;i++)
|
||||
*lower_bound(dp,dp+n,a[i])=a[i];
|
||||
printf("%d\n",lower_bound(dp,dp+n,INF)-dp);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++) scanf("%d",a+i);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
5
|
||||
4 2 3 1 5
|
||||
*/
|
28
history_source/挑战程序设计竞赛/2.3.3.1.cpp
Normal file
28
history_source/挑战程序设计竞赛/2.3.3.1.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include<cstdio>
|
||||
int n,m,M;
|
||||
int dp[1005][1005];
|
||||
void solve()
|
||||
{
|
||||
int i,j;
|
||||
dp[0][0]=1;
|
||||
for(i=1;i<=m;i++)
|
||||
for(j=0;j<=n;j++)
|
||||
if(j-i>=0)
|
||||
dp[i][j]=(dp[i-1][j]+dp[i][j-i])%M;
|
||||
else
|
||||
dp[i][j]=dp[i-1][j];
|
||||
printf("%d\n",dp[m][n]);
|
||||
for(i=0;i<=m;i++)
|
||||
for(j=0;j<=n;j++)
|
||||
printf("dp[%d][%d]=%d\n",i,j,dp[i][j]);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d%d",&n,&m,&M);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
27
history_source/挑战程序设计竞赛/2.3.3.2.cpp
Normal file
27
history_source/挑战程序设计竞赛/2.3.3.2.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include<cstdio>
|
||||
int n,m,M;
|
||||
int a[1001],dp[1001][1001];
|
||||
void solve()
|
||||
{
|
||||
int i,j;
|
||||
for(i=0;i<=n;i++) dp[i][0]=1;
|
||||
for(i=0;i<n;i++)
|
||||
for(j=1;j<=m;j++)
|
||||
if(j-1-a[i]>=0)
|
||||
dp[i+1][j]=(dp[i+1][j-1]+dp[i][j]-dp[i][j-1-a[i]]+M)%M;
|
||||
else
|
||||
dp[i+1][j]=(dp[i+1][j-1]+dp[i][j])%M;
|
||||
printf("%d\n",dp[n][m]);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&n,&m);
|
||||
for(int i=0;i<n;i++) scanf("%d",a+i);
|
||||
scanf("%d",&M);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
49
history_source/挑战程序设计竞赛/2.4.2.1.cpp
Normal file
49
history_source/挑战程序设计竞赛/2.4.2.1.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
int L,P,N;
|
||||
int A[10001],B[10001];
|
||||
void solve()
|
||||
{
|
||||
A[N]=L;
|
||||
B[N]=0;
|
||||
N++;
|
||||
priority_queue<int>que;
|
||||
int ans=0,pos=0,tank=P;
|
||||
for(int i=0;i<N;i++)
|
||||
{
|
||||
int d=A[i]-pos;
|
||||
while(tank-d<0)
|
||||
{
|
||||
if(que.empty())
|
||||
{
|
||||
puts("-1");
|
||||
return;
|
||||
}
|
||||
tank+=que.top();
|
||||
que.pop();
|
||||
ans++;
|
||||
}
|
||||
tank-=d;
|
||||
pos=A[i];
|
||||
que.push(B[i]);
|
||||
}
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d%d",&N,&L,&P);
|
||||
int i;
|
||||
for(i=0;i<N;i++) scanf("%d",A+i);
|
||||
for(i=0;i<N;i++) scanf("%d",B+i);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
4 25 10
|
||||
10 14 20 21
|
||||
10 5 2 4
|
||||
*/
|
13
history_source/挑战程序设计竞赛/2.4.4.1.cpp
Normal file
13
history_source/挑战程序设计竞赛/2.4.4.1.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include<cstdio>
|
||||
void solve()
|
||||
{
|
||||
|
||||
}
|
||||
void init()
|
||||
{
|
||||
|
||||
}
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
43
history_source/挑战程序设计竞赛/2.5.3.1.cpp
Normal file
43
history_source/挑战程序设计竞赛/2.5.3.1.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include<cstdio>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
int n,s;
|
||||
vector<int> G[1001];
|
||||
int color[1001];
|
||||
void init()
|
||||
{
|
||||
int a,b;
|
||||
scanf("%d%d",&n,&s);
|
||||
for(int i=0;i<s;i++)
|
||||
{
|
||||
scanf("%d%d",&a,&b);
|
||||
G[a].push_back(b);G[b].push_back(a);
|
||||
}
|
||||
}
|
||||
bool dfs(int v,int c)
|
||||
{
|
||||
color[v]=c;
|
||||
for(int i=0;i<G[v].size();i++)
|
||||
{
|
||||
if(color[G[v][i]]==c) return 0;
|
||||
if(color[G[v][i]]==0&&!dfs(G[v][i],-c)) return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
for(int i=0;i<n;i++)
|
||||
if(color[i]==0)
|
||||
if(!dfs(i,1))
|
||||
{
|
||||
printf("No\n");
|
||||
return;
|
||||
}
|
||||
printf("Yes\n");
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
67
history_source/挑战程序设计竞赛/2.5.6.1.cpp
Normal file
67
history_source/挑战程序设计竞赛/2.5.6.1.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
#include<cstdio>
|
||||
#include<vector>
|
||||
#include<queue>
|
||||
#include<algorithm>
|
||||
#include<utility>
|
||||
using namespace std;
|
||||
struct edge
|
||||
{
|
||||
int to,cost;
|
||||
edge(){}
|
||||
edge(int t,int c)
|
||||
{
|
||||
to=t;
|
||||
cost=c;
|
||||
}
|
||||
};
|
||||
#define P pair<int,int>
|
||||
int n,r;
|
||||
vector<edge> G[5001];
|
||||
int dist[5001],dist2[5001];
|
||||
const int oo=1<<30;
|
||||
void solve()
|
||||
{
|
||||
priority_queue<P,vector<P>,greater<P> > que;
|
||||
fill(dist,dist+n,oo);
|
||||
fill(dist2,dist2+n,oo);
|
||||
dist[0]=0;
|
||||
que.push(P(0,0));
|
||||
while(!que.empty())
|
||||
{
|
||||
P p=que.top();que.pop();
|
||||
int v=p.second,d=p.first;
|
||||
if(dist2[v]<d) continue;
|
||||
for(int i=0;i<G[v].size();i++)
|
||||
{
|
||||
edge e=G[v][i];
|
||||
int d2=d+e.cost;
|
||||
if(dist[e.to]>d2)
|
||||
{
|
||||
swap(dist[e.to],d2);
|
||||
que.push(make_pair(dist[e.to],e.to));
|
||||
}
|
||||
if(dist2[e.to]>d2&&dist[e.to]<d2)
|
||||
{
|
||||
dist2[e.to]=d2;
|
||||
que.push(P(dist2[e.to],e.to));
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d\n",dist2[n-1]);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int f,t,c;
|
||||
scanf("%d%d",&n,&r);
|
||||
for(int i=0;i<r;i++)
|
||||
{
|
||||
scanf("%d%d%d",&f,&t,&c);
|
||||
G[f].push_back(edge(t,c));
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
56
history_source/挑战程序设计竞赛/2.5.6.2.cpp
Normal file
56
history_source/挑战程序设计竞赛/2.5.6.2.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
#define M 100009
|
||||
struct edge
|
||||
{
|
||||
int from,to,cost;
|
||||
};
|
||||
int n,m,r,ans;
|
||||
int p[M];
|
||||
edge e[M];
|
||||
int cmp(edge a,edge b)
|
||||
{
|
||||
return a.cost<b.cost;
|
||||
}
|
||||
int find(int x)
|
||||
{
|
||||
return x==p[x]?x:p[x]=find(p[x]);
|
||||
}
|
||||
int kruskal()
|
||||
{
|
||||
for(int i=0;i<n+m;i++)
|
||||
p[i]=i;
|
||||
for(int i=0;i<r;i++)
|
||||
{
|
||||
int x=find(e[i].from);
|
||||
int y=find(e[i].to);
|
||||
if(x!=y)
|
||||
{
|
||||
p[x]=y;
|
||||
ans+=e[i].cost;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
ans = 0;
|
||||
scanf("%d %d %d",&n,&m,&r);
|
||||
for(int i = 0;i < r;i++)
|
||||
{
|
||||
int a,b,c;
|
||||
scanf("%d %d %d",&a,&b,&c);
|
||||
e[i] = (edge){a,b+n,-c};
|
||||
}
|
||||
sort(e,e+r,cmp);
|
||||
printf("%d\n",10000*(n+m)+kruskal());
|
||||
}
|
||||
return 0;
|
||||
}
|
41
history_source/挑战程序设计竞赛/2.5.6.3.cpp
Normal file
41
history_source/挑战程序设计竞赛/2.5.6.3.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int oo=1<<30;
|
||||
int ML,MD,N;
|
||||
int AL[10001],BL[10001],DL[10001];
|
||||
int AD[10001],BD[10001],DD[10001];
|
||||
int d[10001];
|
||||
void solve()
|
||||
{
|
||||
fill(d,d+N,oo);
|
||||
d[0]=0;
|
||||
for(int k=0;k<N;k++)
|
||||
{
|
||||
for(int i=0;i+1<N;i++)
|
||||
if(d[i+1]<oo) d[i]=min(d[i],d[i+1]);
|
||||
for(int i=0;i<ML;i++)
|
||||
if(d[AL[i]-1]<oo) d[BL[i]-1]=min(d[BL[i]-1],d[AL[i]-1]+DL[i]);
|
||||
for(int i=0;i<MD;i++)
|
||||
if(d[BD[i]-1]<oo) d[AD[i]-1]=min(d[AD[i]-1],d[BD[i]-1]-DD[i]);
|
||||
}
|
||||
int res=d[N-1];
|
||||
if(d[0]<0) res=-1;
|
||||
else if(res==oo) res=-2;
|
||||
printf("%d\n",res);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d%d",&N,&ML,&MD);
|
||||
int a,b,c;
|
||||
for(int i=0;i<ML;i++)
|
||||
scanf("%d%d%d",AL+i,BL+i,DL+i);
|
||||
for(int i=0;i<MD;i++)
|
||||
scanf("%d%d%d",AD+i,BD+i,DD+i);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
22
history_source/挑战程序设计竞赛/2.6.1.1.cpp
Normal file
22
history_source/挑战程序设计竞赛/2.6.1.1.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include<cstdio>
|
||||
inline int abs(int a)
|
||||
{return a<0?-a:a;}
|
||||
inline int gcd(int a,int b)
|
||||
{return b==0?a:gcd(b,a%b);}
|
||||
int x1,x2,y1,y2;
|
||||
void solve()
|
||||
{
|
||||
int x=abs(x1-x2),y=abs(y1-y2);
|
||||
if(x==0&&y==0) printf("%d\n");
|
||||
else printf("%d\n",gcd(x,y)-1);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
30
history_source/挑战程序设计竞赛/2.6.1.2.cpp
Normal file
30
history_source/挑战程序设计竞赛/2.6.1.2.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include<cstdio>
|
||||
int egcd(int a,int b,int &x,int &y)
|
||||
{
|
||||
static int d;
|
||||
d=a;
|
||||
if(b!=0)
|
||||
{
|
||||
d=egcd(b,a%b,y,x);
|
||||
y-=(a/b)*x;
|
||||
}
|
||||
else x=1,y=0;
|
||||
return d;
|
||||
}
|
||||
int a,b;
|
||||
void solve()
|
||||
{
|
||||
int x,y;
|
||||
if(egcd(a,b,x,y)>1) printf("-1\n");
|
||||
else printf("%d %d %d %d\n",x>=0?x:0,x<0?-x:0,y>=0?y:0,y<0?-y:0);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&a,&b);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
13
history_source/挑战程序设计竞赛/2.6.2.1.cpp
Normal file
13
history_source/挑战程序设计竞赛/2.6.2.1.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include<cstdio>
|
||||
int n;
|
||||
bool isp(int n)
|
||||
{
|
||||
for(int i=2;i*i<=n;i++) if(n%i==0) return 0;
|
||||
return n>1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
printf("%s\n",isp(n)?"Yes":"No");
|
||||
return 0;
|
||||
}
|
27
history_source/挑战程序设计竞赛/2.6.2.2.cpp
Normal file
27
history_source/挑战程序设计竞赛/2.6.2.2.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
int n;
|
||||
bool isp[1000005];
|
||||
void solve()
|
||||
{
|
||||
int p=0,i,j;
|
||||
for(i=2;i<=n;i++)
|
||||
if(isp[i])
|
||||
{
|
||||
p++;
|
||||
for(j=2;j*i<=n;j++) isp[j*i]=0;
|
||||
}
|
||||
printf("%d\n",p);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
memset(isp,1,sizeof(isp));
|
||||
isp[0]=isp[1]=0;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
30
history_source/挑战程序设计竞赛/2.6.2.3.cpp
Normal file
30
history_source/挑战程序设计竞赛/2.6.2.3.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include<cstdio>
|
||||
#include<cmath>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
#define ll long long
|
||||
ll a,b;
|
||||
bool is_prime[1000001],is_prime_small[1000001];
|
||||
void solve()
|
||||
{
|
||||
ll i,j,ans=0;
|
||||
memset(is_prime_small,1,sqrt(b));
|
||||
memset(is_prime,1,b-a);
|
||||
for(i=2;i*i<b;i++)
|
||||
if(is_prime_small[i])
|
||||
{
|
||||
for(j=2*i;j*j<b;j+=i) is_prime_small[j]=0;
|
||||
for(j=max(2LL,(a+i-1)/i)*i;j<b;j+=i) is_prime[j-a]=0;
|
||||
}
|
||||
for(i=a;i<b;i++) ans+=is_prime[i-a];
|
||||
printf("%lld\n",ans);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%lld%lld",&a,&b);
|
||||
solve();
|
||||
return 0;
|
||||
}
|
||||
//22 37
|
||||
//22801763489 22801787297
|
33
history_source/挑战程序设计竞赛/2.6.4.1.cpp
Normal file
33
history_source/挑战程序设计竞赛/2.6.4.1.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include<cstdio>
|
||||
#define ll long long
|
||||
ll n;
|
||||
bool isp(ll n)
|
||||
{
|
||||
static ll i;
|
||||
for(i=2;i*i<=n;i++) if(n%i==0) return 0;
|
||||
return n>1;
|
||||
}
|
||||
ll mod_pow(ll x,ll n,ll mod)
|
||||
{
|
||||
static ll res;
|
||||
res=1;
|
||||
while(n>0)
|
||||
{
|
||||
if(n&1) res=res*x%mod;
|
||||
x=x*x%mod;
|
||||
n>>=1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
int solve()
|
||||
{
|
||||
if(isp(n)) return printf("No\n");
|
||||
for(ll i=2;i<n;i++) if(mod_pow(i,n,n)!=i) return printf("No\n");
|
||||
printf("Yes\n");
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%lld",&n);
|
||||
solve();
|
||||
return 0;
|
||||
}
|
28
history_source/挑战程序设计竞赛/2.7.1.cpp
Normal file
28
history_source/挑战程序设计竞赛/2.7.1.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
inline bool cmp(long long a,long long b)
|
||||
{
|
||||
return a>b;
|
||||
}
|
||||
long long v1[805],v2[805],n,ans;
|
||||
int main()
|
||||
{
|
||||
scanf("%lld",&n);
|
||||
for(int i=0;i<n;i++) scanf("%lld",v1+i);
|
||||
for(int i=0;i<n;i++) scanf("%lld",v2+i);
|
||||
sort(v1,v1+n);
|
||||
sort(v2,v2+n,cmp);
|
||||
for(int i=0;i<n;i++) ans+=v1[i]*v2[i];
|
||||
// for(int i=0;i<n;i++) printf("%lld ",v1[i]);
|
||||
// printf("\n");
|
||||
// for(int i=0;i<n;i++) printf("%lld ",v2[i]);
|
||||
// printf("\n");
|
||||
printf("%lld\n",ans);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
3
|
||||
1 3 -5
|
||||
-2 4 1
|
||||
*/
|
44
history_source/挑战程序设计竞赛/2.7.2.cpp
Normal file
44
history_source/挑战程序设计竞赛/2.7.2.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int n,M[45][45],a[45];
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
int i,j;
|
||||
for(i=0;i<n;i++)
|
||||
for(j=0;j<n;j++)
|
||||
scanf("%d",&M[i][j]);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int res=0;
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
a[i]=-1;
|
||||
for(int j=0;j<n;j++)
|
||||
if(M[i][j]==1) a[i]=j;
|
||||
}
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
int pos=-1;
|
||||
for(int j=i;j<n;j++)
|
||||
if(a[j]<=i)
|
||||
{
|
||||
pos=j;
|
||||
break;
|
||||
}
|
||||
for(int j=pos;j>i;j--)
|
||||
{
|
||||
swap(a[j],a[j-1]);
|
||||
res++;
|
||||
}
|
||||
}
|
||||
printf("%d\n",res);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
32
history_source/挑战程序设计竞赛/2.7.3.cpp
Normal file
32
history_source/挑战程序设计竞赛/2.7.3.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int p,q,a[105];
|
||||
int dp[105][105];
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&p,&q);
|
||||
for(int i=1;i<=q;i++) scanf("%d",a+i);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
a[0]=0;
|
||||
a[q+1]=p+1;
|
||||
for(int i=0;i<=q;i++) dp[i][i+1]=0;
|
||||
for(int w=2;w<=q+1;w++)
|
||||
{
|
||||
for(int i=0;i+w<=q+1;i++)
|
||||
{
|
||||
int j=i+w,t=1<<30;for(int k=i+1;k<j;k++)
|
||||
t=min(t,dp[i][k]+dp[k][j]);
|
||||
dp[i][j]=t+a[j]-a[i]-2;
|
||||
}
|
||||
}
|
||||
printf("%d\n",dp[0][q+1]);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
38
history_source/挑战程序设计竞赛/2.7.4.cpp
Normal file
38
history_source/挑战程序设计竞赛/2.7.4.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int m,x;
|
||||
double p;
|
||||
double dp[2][(1<<15)+1];
|
||||
void init()
|
||||
{
|
||||
scanf("%d%lf%d",&m,&p,&x);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int n=1<<m;
|
||||
double *prv=dp[0],*nxt=dp[1];
|
||||
memset(prv,0,sizeof(double)*(n+1));
|
||||
prv[n]=1.0;
|
||||
for(int r=0;r<m;r++)
|
||||
{
|
||||
for(int i=0;i<=n;i++)
|
||||
{
|
||||
int jub=min(i,n-i);
|
||||
double t=0.0;
|
||||
for(int j=0;j<=jub;j++)
|
||||
t=max(t,p*prv[i+j]+(1-p)*prv[i-j]);
|
||||
nxt[i]=t;
|
||||
}
|
||||
swap(prv,nxt);
|
||||
}
|
||||
int i=(long long)x*n/1000000;
|
||||
printf("%.6lf\n",prv[i]);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
65
history_source/挑战程序设计竞赛/2.e.1.cpp
Normal file
65
history_source/挑战程序设计竞赛/2.e.1.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
#include<cstdio>
|
||||
#include<queue>
|
||||
//#include<mydef/screen.h>
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int x,y;
|
||||
node()
|
||||
{
|
||||
x=y=0;
|
||||
}
|
||||
node(int X,int Y)
|
||||
{
|
||||
x=X;
|
||||
y=Y;
|
||||
}
|
||||
};
|
||||
queue<node> que;
|
||||
int w,h,x,y,ans;
|
||||
char map[25][25];
|
||||
int dx[]={0,0,-1,1},dy[]={-1,1,0,0};
|
||||
void solve()
|
||||
{
|
||||
while(que.size()>0) que.pop();
|
||||
ans=1;
|
||||
map[y][x]='#';
|
||||
que.push(node(x,y));
|
||||
node t;
|
||||
while(que.size()>0)
|
||||
{
|
||||
t=que.front();
|
||||
que.pop();
|
||||
for(int i=0;i<4;i++)
|
||||
if(t.x+dx[i]>=0&&t.x+dx[i]<w&&t.y+dy[i]>=0&&t.y+dy[i]<h&&map[t.y+dy[i]][t.x+dx[i]]=='.')
|
||||
{
|
||||
map[t.y+dy[i]][t.x+dx[i]]='#';
|
||||
ans++;
|
||||
que.push(node(t.x+dx[i],t.y+dy[i]));
|
||||
}
|
||||
}
|
||||
// SetColor(10);
|
||||
printf("%d\n",ans);
|
||||
// SetColor(15);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j;
|
||||
while(1)
|
||||
{
|
||||
scanf("%d%d",&w,&h);
|
||||
if(w==0||h==0) break;
|
||||
getchar();
|
||||
for(i=0;i<h;i++)
|
||||
{
|
||||
for(j=0;j<w;j++)
|
||||
{
|
||||
map[i][j]=getchar();
|
||||
if(map[i][j]=='@') x=j,y=i;
|
||||
}
|
||||
getchar();
|
||||
}
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
35
history_source/挑战程序设计竞赛/2.e.8.cpp
Normal file
35
history_source/挑战程序设计竞赛/2.e.8.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
int a[15];
|
||||
long long ans=1LL<<62;
|
||||
void test(int pos)
|
||||
{
|
||||
vector<int> b;
|
||||
b.reserve(a[0]-2);
|
||||
long long v1=a[pos],v2=a[pos+1];
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
a[0]=0;
|
||||
while(a[a[0]+1]=getchar()-'0',a[a[0]+1]!='\n'-'0')
|
||||
if(a[a[0]+1]>=0&&a[a[0]+1]<=9) a[0]++;
|
||||
sort(a+1,a+a[0]+1);
|
||||
int m=100;
|
||||
for(int i=1;i<a[0];i++)
|
||||
m=min(m,a[i+1]-a[i]);
|
||||
if(a[0]%2==0)
|
||||
{
|
||||
for(int i=1;i<a[0];i++)
|
||||
if(a[i+1]-a[i]==m) test(i);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d",&n);
|
||||
getchar();
|
||||
while(n-->0) solve();
|
||||
return 0;
|
||||
}
|
32
history_source/挑战程序设计竞赛/3.1.1.cpp
Normal file
32
history_source/挑战程序设计竞赛/3.1.1.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include<cstdio>
|
||||
int n,k;
|
||||
int a[1000005];
|
||||
void solve()
|
||||
{
|
||||
int l=0,r=n-1,mid;
|
||||
while(l<r)
|
||||
{
|
||||
mid=(l+r)/2;
|
||||
if(a[mid]<k) l=mid+1;
|
||||
else r=mid;
|
||||
}
|
||||
if(a[l]>=k) printf("%d\n",l);
|
||||
else printf("-1");
|
||||
}
|
||||
void init()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++) scanf("%d",a+i);
|
||||
scanf("%d",&k);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
5
|
||||
2 3 3 5 6
|
||||
3
|
||||
*/
|
39
history_source/挑战程序设计竞赛/3.1.2.cpp
Normal file
39
history_source/挑战程序设计竞赛/3.1.2.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int n,k;
|
||||
double l[10005];
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&n,&k);
|
||||
for(int i=0;i<n;i++) scanf("%lf",l+i);
|
||||
}
|
||||
bool c(double len)
|
||||
{
|
||||
int cnt=0;
|
||||
for(int i=0;i<n;i++)
|
||||
cnt+=l[i]/len;
|
||||
return (cnt>=k);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
double l=0,r=100000,mid;
|
||||
while(r-l>0.001)
|
||||
{
|
||||
mid=(l+r)/2;
|
||||
if(c(mid)) l=mid;
|
||||
else r=mid;
|
||||
}
|
||||
printf("%.2lf\n",l);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
4
|
||||
11
|
||||
8.02 7.43 4.57 5.39
|
||||
*/
|
44
history_source/挑战程序设计竞赛/3.1.3.cpp
Normal file
44
history_source/挑战程序设计竞赛/3.1.3.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int n,m,x[100005];
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&n,&m);
|
||||
for(int i=0;i<n;i++) scanf("%d",x+i);
|
||||
}
|
||||
bool c(int d)
|
||||
{
|
||||
int last=0,crt;
|
||||
for(int i=1;i<m;i++)
|
||||
{
|
||||
crt=last+1;
|
||||
while(crt<n&&x[crt]-x[last]<d) crt++;
|
||||
if(crt==n) return 0;
|
||||
last=crt;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
sort(x,x+n);
|
||||
int l=0,r=1<<30,mid;
|
||||
while(r-l>1)
|
||||
{
|
||||
mid=(l+r)/2;
|
||||
if(c(mid)) l=mid;
|
||||
else r=mid;
|
||||
}
|
||||
printf("%d\n",l);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
5
|
||||
3
|
||||
1 2 8 4 9
|
||||
*/
|
41
history_source/挑战程序设计竞赛/3.1.4.cpp
Normal file
41
history_source/挑战程序设计竞赛/3.1.4.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int n,k,w[10005],v[10005];
|
||||
double y[10005];
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&n,&k);
|
||||
for(int i=0;i<n;i++) scanf("%d%d",w+i,v+i);
|
||||
}
|
||||
bool C(double x)
|
||||
{
|
||||
for(int i=0;i<n;i++) y[i]=v[i]-x*w[i];
|
||||
sort(y,y+n);
|
||||
double sum=0;
|
||||
for(int i=0;i<k;i++) sum+=y[n-i-1];
|
||||
return sum>=0;
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
double l=0,r=1000010,mid;
|
||||
for(int i=0;i<100;i++)
|
||||
{
|
||||
mid=(l+r)/2;
|
||||
if(C(mid)) l=mid;
|
||||
else r=mid;
|
||||
}
|
||||
printf("%.2f\n",r);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
3 2
|
||||
2 2
|
||||
5 3
|
||||
2 1
|
||||
*/
|
35
history_source/挑战程序设计竞赛/3.2.1.1.cpp
Normal file
35
history_source/挑战程序设计竞赛/3.2.1.1.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int n,S;
|
||||
int a[100005];
|
||||
void init()
|
||||
{
|
||||
scanf("%d%d",&n,&S);
|
||||
for(int i=0;i<n;i++) scanf("%d",a+i);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int l,r,sum,res=n+1;
|
||||
l=r=sum=0;
|
||||
while(1)
|
||||
{
|
||||
while(r<n&&sum<S) sum+=a[r++];
|
||||
if(sum<S) break;
|
||||
res=min(res,r-l);
|
||||
sum-=a[l++];
|
||||
}
|
||||
if(res>n) printf("0\n");
|
||||
else printf("%d\n",res);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d",&n);
|
||||
while(n-->0)
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user