2012 winter training @HIT Day 4 解题报告
冬训第四天,练习贪心。春节之前最后一天的contest了,只有三道。
http://acm.hit.edu.cn/hoj/contest/view?id=100130
Problem A:Mixing Milk
hoj1003,以前刷过。就是贪心,挑最便宜的即可。
/*This Code is Submitted by acehypocrisy for Problem 4000095 at 2012-01-20 23:04:02*/ /*This Code is Submitted by acehypocrisy for Problem 1003 at 2011-09-19 09:24:59*/ #include <stdio.h> int main(int argc, char *argv[]) { int total,n; scanf("%d %d",&total,&n); int farmers[5000][2]; int q; for (q=0;q<n;q++) scanf("%d %d",&farmers[q][1],&farmers[q][2]); int i,j,tmp,flag; for (i=0;i<n-1;i++){ flag=1; for (j=0;j<n-i-1;j++){ if (farmers[j][1]>farmers[j+1][1]){ tmp=farmers[j][1]; farmers[j][1]=farmers[j+1][1]; farmers[j+1][1]=tmp; tmp=farmers[j][2]; farmers[j][2]=farmers[j+1][2]; farmers[j+1][2]=tmp; flag=0; } } if (1==flag) break; } int sum=0,num=0,k=0; for (k=0;k<n;k++){ if (num+farmers[k][2]<total){ num+=farmers[k][2]; sum+=farmers[k][1]*farmers[k][2]; }else{ sum+=(total-num)*farmers[k][1]; break; } } printf("%d\n",sum); return 0; }
Problem B:数列极差问题
每次删掉最小的两个,最后的数就是最大的;每次删掉最大的两个,最后的数就是最小的。
*This Code is Submitted by acehypocrisy for Problem 4000096 at 2012-01-21 13:34:06*/ #include <stdio.h> #include <algorithm> using namespace std; bool compare (const int& a, const int& b){ return a > b; } int main() { int N; int sequence1[50000], sequence2[50000]; while(scanf("%d", &N) == 1){ if (N == 0) break; for (int i = 0; i < N; i++){ scanf("%d", &sequence1[i]); sequence2[i] = sequence1[i]; } while (N > 1){ sort(sequence1, sequence1 + N); sort(sequence2, sequence2 + N, compare); sequence1[N - 2] = sequence1[N - 2] * sequence1[N - 1] + 1; sequence2[N - 2] = sequence2[N - 2] * sequence2[N - 1] + 1; N--; } printf("%d\n", sequence2[0] - sequence1[0]); } return 0; }
Problem C:TEX Quotes
1101,以前也刷过……其实基本上应该没有太大的难度。
代码好像有点繁琐,其实一个字符一个字符的判断之后直接print出去就行,不必用还用string什么的。。当时可能没想到把。
cur指针记录的是从上一个 “ 到下一个 ” 之间的字符串。
/*This Code is Submitted by acehypocrisy for Problem 4000097 at 2012-01-20 23:02:56*/ /*This Code is Submitted by acehypocrisy for Problem 1101 at 2011-10-30 11:56:28*/ #include <cstdlib> #include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { char temp[10000]; string s=""; int counter=0; while (cin.getline(temp,10000)){ char* p=temp; char* cur=temp; while ((*p)!='\0'){ if ((*p)=='\"'){ counter++; (*p)='\0'; s+=cur; if (counter%2==1){ s+="``"; }else{ s+="''"; } p++; cur=p; continue; } p++; } s+=cur; cout<<s<<endl; s=""; } return 0; }