CCF机试准备20170916
来源:互联网 发布:java打印功能代码 编辑:程序博客网 时间:2024/05/21 11:00
题目:集合竞价
#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 1001typedef 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,将根指向另一棵树的根,而不是将叶子指向叶子,因为叶子已经有指向了,再指向其他就把之前数据覆盖掉了,也不是把根指向叶子,根指向根会减小树的深度,使找根的速度加快。
阅读全文
0 0
- CCF机试准备20170916
- #CCF准备一年日常刷题#201403相反数
- #CCF准备一年日常刷题#201412-1 门禁系统
- #CCF准备一年日常刷题#201312-2 ISBN号码
- #CCF准备一年日常刷题#201503-1图像旋转
- #CCF准备一年日常刷题#201403-2窗口
- #CCF准备一年日常刷题#201509-1 数列分段
- #CCF准备一年日常刷题#201512-1 数位之和
- #CCF准备一年日常刷题#201409-2 画图
- #CCF准备一年日常刷题#201609-1 最大波动
- #CCF准备一年日常刷题#201612-1 中间数
- #CCF准备一年日常刷题#201703-1 分蛋糕
- #CCF准备一年日常刷题#201503-2 数字排序
- #CCF准备一年日常刷题#201509-2 日期计算
- ccf
- CCF
- ccf
- CCF机试笔记20170905
- 使用lua脚本做高并发限流
- GreenDAO使用
- 随记
- HDU1056
- jQuery做图片轮播
- CCF机试准备20170916
- eclipse不显示代码提示的解决方案
- HTTP缓存相关头
- 面试的知识总结和技巧
- GGH97
- Session机制、持久化、session="false"属性不创建session、显示创建session及其销毁
- Spring web.xml
- HDU1058
- 使用UI REST API采集Storm指标