题目:集合竞价
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct test
{
char op[10];
double p;
long long s;
}buff[5001],buy[5001],sell[5001];
int flag[5001];
bool cmp1(const struct test &a,const struct test &b)
{
if(a.p!=b.p)
return a.p>b.p;
else
return a.s>b.s;
}
bool cmp2(const struct test &a,const struct test &b)
{
if(a.p!=b.p)
return a.p<b.p;
else
return a.s>b.s;
}
int main()
{
int i = 0, f;
double b1;
long long b2;
char a[10];
freopen("text.txt","r",stdin);
while(scanf("%s", a)!=EOF)
{
strcpy(buff[i].op,a);
if(!strcmp(a,"cancel"))
{
scanf("%I64d",&buff[i].s);
flag[buff[i].s-1]=1;
flag[i]=1;
}
else{
scanf("%lf", &buff[i].p);
scanf("%I64d", &buff[i].s);
}
i++;
}
int size1=0, size2=0;
for(int j = 0; j < i; j++)
{
if(flag[j]!=1)
{
if(!strcmp(buff[j].op,"buy"))
{
buy[size1].p=buff[j].p;
buy[size1++].s=buff[j].s;
}
else{
sell[size2].p=buff[j].p;
sell[size2++].s=buff[j].s;
}
}
}
sort(buy,buy+size1, cmp1);
sort(sell, sell+size2, cmp2);
long long sum1=0, sum2=0, anssum=0;
double ansprice;
for(int i=0; i<size1; i++)
{
int f = 0, j;
sum1+=buy[i].s;
sum2 = 0;
for(j=0; j<size2; j++)
{
if(sell[j].p>buy[i].p) {f = 1; break;}
else
sum2+=sell[j].s;
}
if((f==1)||(j==size2))
{
long long min = (sum1>sum2)?sum2:sum1;
if(anssum<min)
{
anssum = min;
ansprice = buy[i].p;
}
}
}
printf("%.2lf %I64d\n",ansprice, anssum);
return 0;
}
注意:不要忽视了cancel i 那一行的flag值也要设为1,还有cancel读入的是要删除的行,所以要根据读入的i设置flag[i-1]的值为1。所以一个cancel要设置两个flag=1。
201604-3:路径解析
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
freopen("text.txt","r",stdin);
string curDIR;
int n;
cin>>n>>curDIR;
cin.ignore();
for(int i=0; i<n; i++)
{
string line;
int begin = 0;
getline(cin,line);
if(line.size()==0)
line = curDIR;
else if((line[0]!='/')) //相对路径
line = curDIR+"/"+line;
//去掉//
while((begin = line.find("//",0))!=string::npos)
line.erase(begin,1);
//去掉/../
while((begin = line.find("/../",0))!=string::npos)
{
if(begin==0)
line.erase(begin,3);
else{
int i = begin-1;
while(line.find('/',i)==begin)
{
i--;
}
line.erase(i,begin-i+3);
//cout<<line<<endl;
}
}
//去掉/./
while((begin = line.find("/./",0))!=string::npos)
line.erase(begin,2);
//去掉最后的/
if((line[line.size()-1]=='/')&&line.size()!=1)
line.erase(line.size()-1,1);
cout<<line<<endl;
}
return 0;
}
注意:
- 一定要注意处理的顺序,去掉最后的/应该放在最后,否则/d1/d3/../这样的路径将无法被处理,还有//处理应该放在/../之前,否则curDIR=/d2/d3/../,相对路径是../d4/f1时,连接起来就是/d2/d3/..//../d4/f1,将有一个路径不会被处理。
- 还有就是要加入cin.ignore(),否则getline(cin,line)会读入上一行遗留的\n而少读入一行。
最优灌溉
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
#define MAX 1001
typedef struct { int s, e, c;}edges;
vector<edges> V;
int parent[MAX];
bool cmp(const edges &a, const edges &b)
{
return a.c<b.c;
}
int find_parent(int parent[], int x)
{
if(parent[x]==-1) return x;
else
return find_parent(parent, parent[x]);
}
int main()
{
int n, m;
long long sum = 0;
cin>>n>>m;
for(int i=1; i<=n; i++)
parent[i]=-1;
for(int i=0; i<m; i++)
{
edges tmp;
cin>>tmp.s>>tmp.e>>tmp.c;
V.push_back(tmp);
}
sort(V.begin(), V.end(), cmp);
for(int i=0; i<m; i++)
{
int root_s = find_parent(parent, V[i].s);
int root_e = find_parent(parent, V[i].e);
if(root_e!=root_s)
{
parent[root_e]=root_s;//parent[V[i].e]=V[i].s;
sum+=V[i].c;
}
}
cout<<sum<<endl;
return 0;
}
注意:不禁感叹,学长真是太机智了!parent[root_e]=root_s,将根指向另一棵树的根,而不是将叶子指向叶子,因为叶子已经有指向了,再指向其他就把之前数据覆盖掉了,也不是把根指向叶子,根指向根会减小树的深度,使找根的速度加快。