1.3.1 FatMouse' Trade
优先性价比高的,明显的贪心
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int m,n;
cout<<fixed<<setprecision(3);
cin>>m>>n;
while (m!=-1 || n!=-1)
{
int a[1000],f[1000];
double ave[1000];
for (int i=0;i<n;i++)
{
cin>>a[i]>>f[i];
ave[i]=(double)a[i]/(double)f[i];
}
int x;
double t;
for (int i=1;i<n;i++)
for (int j=0;j<n-i;j++)
if (ave[j]<ave[j+1])
{
t=ave[j]; ave[j]=ave[j+1]; ave[j+1]=t;
x=a[j]; a[j]=a[j+1]; a[j+1]=x;
x=f[j]; f[j]=f[j+1]; f[j+1]=x;
}
int j=0;
double ans=0;
while (m>0 && j<n)
{
if (m>=f[j])
{
ans+=a[j];
m-=f[j];
}
else
{
ans+=(double)m*(double)a[j]/(double)f[j];
m=0;
}
j++;
}
cout<<ans<<endl;
cin>>m>>n;
}
}
1.3.2 Tian Ji -- The Horse Racing
比较恶心的一个贪心,四种情况讨论
#include<iostream>
using namespace std;
int main()
{
int n;
int a[1000],b[1000];
cin>>n;
while (n!=0)
{
for (int i=0;i<n;i++)
cin>>a[i];
for (int i=0;i<n;i++)
cin>>b[i];
int t,ans=0;
for (int i=1;i<n;i++)
for (int j=0;j<n-i;j++)
{
if (a[j]<a[j+1]) {t=a[j]; a[j]=a[j+1]; a[j+1]=t;}
if (b[j]<b[j+1]) {t=b[j]; b[j]=b[j+1]; b[j+1]=t;}
}
int l1=0,l2=0,r1=n-1,r2=n-1;
while (l1<=r1)
{
if (a[l1]>b[l2])
{
ans+=200;
l1++; l2++;
}
else
if (a[l1]<b[l2])
{
ans-=200;
r1--; l2++;
}
else
if (a[l1]==b[l2] && a[r1]>b[r2])
{
ans+=200;
r1--; r2--;
}
else
if (a[l1]==b[l2] && a[r1]<=b[r2])
{
if (a[r1]<b[l2]) ans-=200;
r1--; l2++;
}
}
cout<<ans<<endl;
cin>>n;
}
}
1.3.3 百步穿杨
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
for (int tt=0;tt<t;tt++)
{
int n,swap;
int a[50],b[50];
cin>>n;
for (int i=0;i<n;i++)
cin>>a[i]>>b[i];
for (int i=1;i<n;i++)
for (int j=0;j<n-i;j++)
if (a[j]>a[j+1])
{
swap=a[j]; a[j]=a[j+1]; a[j+1]=swap;
swap=b[j]; b[j]=b[j+1]; b[j+1]=swap;
}
for (int i=0;i<n;i++)
{
for (int j=0;j<b[i];j++)
{
cout<<">+";
for (int k=0;k<a[i]-2;k++)
cout<<"-";
cout<<"+>"<<endl;
}
cout<<endl;
}
}
return 0;
}
1.3.4 开门人和关门人
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
cin>>n;
for (int tt=0;tt<n;tt++)
{
int m,mi,ma;
string name[1000],time1,time2;
cin>>m;
string min="00:00:00",max="23:59:59";
for (int i=0;i<m;i++)
{
cin>>name[i];
cin>>time1;
if (time1<=max) {max=time1; ma=i;}
cin>>time2;
if (time2>=min) {min=time2; mi=i;}
}
cout<<name[ma]<<" "<<name[mi]<<endl;
}
return 0;
}
1.3.5 排序
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string st;
while (cin>>st)
{
int len=st.length(),top=0,t;
int a[1000];
for (int i=0;i<1000;i++) a[i]=0;
int j=0;
while (st[j]=='5') j++;
while (j<len)
{
if (st[j]!='5') a[top]=10*a[top]+(st[j]-'0');
if (st[j]=='5' && st[j+1]!='5' && j+1<len) top++;
j++;
}
for (int i=1;i<=top;i++)
for (int j=0;j<=top-i;j++)
if (a[j]>a[j+1]) {t=a[j]; a[j]=a[j+1]; a[j+1]=t;}
cout<<a[0];
for (int i=1;i<=top;i++)
cout<<" "<<a[i];
cout<<endl;
}
return 0;
}
1.3.6 悼念512汶川大地震遇难同胞——老人是真饿了
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int t;
cout<<fixed<<setprecision(2);
cin>>t;
for (int tt=0;tt<t;tt++)
{
int n,m,swap;
int p[1000],h[1000];
cin>>n>>m;
for (int i=0;i<m;i++)
cin>>p[i]>>h[i];
for (int i=1;i<m;i++)
for (int j=0;j<m-i;j++)
if (p[j]>p[j+1])
{swap=p[j]; p[j]=p[j+1]; p[j+1]=swap;
swap=h[j]; h[j]=h[j+1]; h[j+1]=swap;}
double ans=0;
for (int i=0;i<m;)
{
if (p[i]*h[i]<n)
{
ans+=h[i];
n-=p[i]*h[i];
i++;
}
else
{
ans+=(double)n/(double)p[i];
break;
}
}
cout<<ans<<endl;
}
return 0;
}
1.3.7 Wooden
#include<iostream>
using namespace std;
typedef pair<int ,int> stick;
stick wood[5000];
bool f[5000];
void sort(int l,int r)
{
int s=l,t=r;
stick x=wood[(l+r)/2],y;
while (s<=t)
{
while (wood[s]<x) s++;
while (wood[t]>x) t--;
if (s<=t) {y=wood[t]; wood[t]=wood[s]; wood[s]=y; s++; t--;}
}
if (l<t) sort(l,t);
if (s<r) sort(s,r);
}
int main()
{
int t;
cin>>t;
for (int tt=0;tt<t;tt++)
{
int n;
cin>>n;
for (int i=0;i<n;i++)
{
cin>>wood[i].first>>wood[i].second;
f[i]=false;
}
sort(0,n-1);
int ans=0,tol=0;
while (tol<n)
{
int j=0;
while (f[j] && j<n) j++;
if (j<n)
{
ans++;
tol++;
f[j]=true;
for (int i=j+1;i<n;i++)
if (!f[i] && wood[i].first>=wood[j].first && wood[i].second>=wood[j].second)
{
tol++;
f[i]=true;
j=i;
}
}
}
cout<<ans<<endl;
}
return 0;
}
1.3.8 As Easy As A+B
#include<iostream>
using namespace std;
int a[1000];
void sort(int l,int r)
{
int s=l,t=r,x=a[(l+r)/2],y;
while (s<=t)
{
while (a[s]<x) s++;
while (a[t]>x) t--;
if (s<=t) {y=a[t]; a[t]=a[s]; a[s]=y; s++; t--;}
}
if (l<t) sort(l,t);
if (s<r) sort(s,r);
}
int main()
{
int t;
cin>>t;
for (int tt=0;tt<t;tt++)
{
int n;
cin>>n;
for (int i=0;i<n;i++)
cin>>a[i];
sort(0,n-1);
cout<<a[0];
for (int i=1;i<n;i++)
cout<<" "<<a[i];
cout<<endl;
}
return 0;
}