Add 5 answers.

This commit is contained in:
Kirigaya Kazuto 2017-08-23 17:54:19 +08:00
parent 84069ef85f
commit 1bf5e2bfbd
5 changed files with 381 additions and 0 deletions

134
POJ/2431.cpp Normal file
View File

@ -0,0 +1,134 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
#define MAX_N 10000
int L,P,N;
pair<int,int> X[MAX_N+1];
struct _A
{
public:
int& operator [] (int x)
{
return X[x].first;
}
}A;
struct B
{
public:
int& operator [] (int x)
{
return X[x].second;
}
}B;
void solve()
{
A[N]=L;
B[N]=0;
N++;
priority_queue<int> bus;
int ans=0,pos=0,tank=P;
for(int i=0;i<N;i++)
{
int d=A[i]-pos;
while(tank-d<0)
{
if(bus.empty())
{
printf("-1\n");
return;
}
tank+=bus.top();
bus.pop();
++ans;
}
tank-=d;
pos=A[i];
bus.push(B[i]);
}
printf("%d\n",ans);
}
int main()
{
scanf("%d",&N);
for(int i=0;i<N;i++)
{
scanf("%d %d",&A[i],&B[i]);
}
scanf("%d %d",&L,&P);
for(int i=0;i<N;i++)
{
A[i]=L-A[i];
}
sort(X,X+N);
solve();
}
/*
int main()
{
int n;
scanf("%d",&n);
vector< pair<int,int> > vec;
for(int i=0;i<n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
vec.push_back(make_pair(a,b));
}
int L,P;
scanf("%d %d",&L,&P);
priority_queue<int> bus;
int cnt=0;
int now=0;
int fuel=P;
for(int i=0;i<n;i++)
{
int nextLength=L-vec[i].first-now;
while(fuel-nextLength<0)
{
if(bus.empty())
{
/// Can not reach
printf("-1\n");
return 0;
}
fuel+=bus.top();
bus.pop();
++cnt;
}
fuel=fuel-nextLength;
now=L-vec[i].first;
bus.push(vec[i].second);
}
printf("%d\n",cnt);
return 0;
}
//*/

37
UVA/100.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int l,r;
while(scanf("%d %d",&l,&r)==2)
{
/// Naive! What assume l < r?
int ll=min(l,r);
int rr=max(l,r);
/// F^K. Why the problem does not show the REAL range?
int maxcnt=-1;
for(int i=ll;i<=rr;i++)
{
long long c=i;
int cnt=1;
while(c!=1)
{
if(c&1) c=c*3+1;
else c=c/2;
++cnt;
}
maxcnt=max(maxcnt,cnt);
}
printf("%d %d %d\n",l,r,maxcnt);
}
return 0;
}

90
UVA/10033.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int reg[10];
int mem[1000];
int main()
{
int t;
scanf("%d%*c%*c",&t);
while(t--)
{
memset(reg,0,sizeof(reg));
memset(mem,0,sizeof(mem));
/// Read code
int cc=0;
while(1)
{
string buff;
if(!getline(cin,buff)) break;
if(buff.size()>1)
{
sscanf(buff.c_str(),"%d",&mem[cc]);
++cc;
}
else break;
}
/// Execute Code
int cnt=0;
int pc=0;
while(1)
{
int opt=mem[pc];
++cnt;
++pc;
if(opt==100)
{
break;
}
int d=(opt%100)/10;
int n=opt%10;
switch(opt/100)
{
case 0:
if(reg[n]) pc=reg[d];
break;
case 2:
reg[d]=n;
break;
case 3:
reg[d]=(reg[d]+n)%1000;
break;
case 4:
reg[d]=(reg[d]*n)%1000;
break;
case 5:
reg[d]=reg[n];
break;
case 6:
reg[d]=(reg[d]+reg[n])%1000;
break;
case 7:
reg[d]=(reg[d]*reg[n])%1000;
break;
case 8:
reg[d]=mem[reg[n]];
break;
case 9:
mem[reg[n]]=reg[d];
break;
}
}
printf("%d\n",cnt);
if(t) printf("\n");
}
return 0;
}

56
UVA/10137.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
double v[1010];
int main()
{
int n;
while(scanf("%d",&n)==1&&n)
{
double sum=0;
for(int i=0;i<n;i++)
{
scanf("%lf",&v[i]);
sum+=v[i];
}
double ave=sum/n;
/// Why?
int c=ave*100;
double ave_x=c/100+c%100*0.01;
double ave_f;
if(ave==ave_x)
{
ave_f=ave_x;
}
else
{
ave_f=ave_x+0.01;
}
double totalA=0;
double totalB=0;
for(int i=0;i<n;i++)
{
if(v[i]<=ave)
{
totalA+=ave_x-v[i];
}
else
{
totalB+=v[i]-ave_f;
}
}
double total=max(totalA,totalB);
printf("$%.2f\n",total);
}
return 0;
}

64
UVA/10189.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
char buff[110][110];
int mp[110][110];
int main()
{
int cc=1;
int n,m;
while(scanf("%d %d%*c",&n,&m)==2&&n&&m)
{
if(cc!=1) printf("\n");
printf("Field #%d:\n",cc++);
memset(mp,0,sizeof(mp));
for(int i=1;i<=n;i++)
{
scanf("%s",&buff[i][1]);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(buff[i][j]=='*')
{
mp[i][j]=-999;
++mp[i-1][j-1];
++mp[i-1][j];
++mp[i-1][j+1];
++mp[i][j-1];
++mp[i][j+1];
++mp[i+1][j-1];
++mp[i+1][j];
++mp[i+1][j+1];
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mp[i][j]<0)
{
printf("*");
}
else
{
printf("%d",mp[i][j]);
}
}
printf("\n");
}
}
return 0;
}