A:
UVALive 6084 | Happy Camper |
#include <cstdio> typedef long long ll; int main(){ ll l, v, p; int cas = 0; while(~scanf("%lld%lld%lld", &l, &p, &v)) { if(l == 0 && p == 0 && v == 0) break; ll ans; if(v%p > l) ans = v/p*l+l; else ans = v/p*l+ v%p; printf("Case %d: %lld\n", ++cas, ans); } return 0; }
B:
UVALive 6085 | Chemistry |
题意:
按字典序把每个元素的个数都输出来。
维护个栈,跟括号匹配差不多,写写写。。
#include <cstdio> #include <iostream> #include <string> #include <map> #include <cstring> #include <algorithm> #include <vector> using namespace std; typedef long long ll; typedef map<string, ll> M; typedef pair<string, ll> pii; const int N =100+10; char s[N]; vector<pii> out; void merge(M& a, M b, ll v) { for (M::iterator it = b.begin(); it != b.end(); ++it) a[it->first] += v*it->second; } M cal(char *s, int len) { M re; for (int i = 0; i < len;) { if (s[i] == '(') { int k = 0, j; ll v = 1; for (j = i; j < len; ++j) { if (s[j] == '(') ++ k; if (s[j] == ')') { if (--k == 0) break; } } M g = cal(s+i+1, j-i-1); ++ j; if (s[j] >= '0' && s[j] <= '9') { v = 0; for (; j < len; ++j) if (s[j] >= '0' && s[j] <= '9') { v = v * 10 + s[j] - '0'; } else break; } merge(re, g, v); i = j; } else { string ch = ""; ch += s[i]; int j; ll v = 1; for (j = i + 1; j < len; ++j) { if (s[j] >= 'a' && s[j] <= 'z') ch += s[j]; else break; } if (s[j] >= '0' && s[j] <= '9') { v = 0; for (; j < len; ++j) { if (s[j] >= '0' && s[j] <= '9') v = v * 10+ s[j]-'0'; else break; } } re[ch] += v; i = j; } } return re; } void work() { int len = strlen(s); M ans = cal(s, len); out.clear(); for (M::iterator it = ans.begin(); it != ans.end(); ++it) out.push_back(pii(it->first, it->second)); sort(out.begin(), out.end()); int f = 0; for (int i = 0; i < out.size(); ++i) { if (f) putchar('+'); if (out[i].second > 1) printf("%lld", out[i].second); if (out[i].second > 0) { f = 1; cout << out[i].first ; } } puts(""); } int main() { while (~scanf("%s", s)) work(); return 0; }
UVALive 6087 | Fuel Stops |
题意:
给定n 个加油站
下面n 个数表示每个 加油站能给车子加的油量
下面n个数表示这个加油站开往下一个加油站需要的油量 i->i+1, n->1
问 :
从哪些加油站出发,是不需要额外用油的(即只需要使用加油站提供的油量)
油箱容量无限
思路:
见代码
#include <cstdio> #include <algorithm> using namespace std; typedef long long ll; const int N =100000+5; int T = 0, n, a[N], b[N], ans[N], dep; ll vr[N], mir[N], mil[N]; void work() { for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) scanf("%d", &b[i]); ll v = 0, mi = 0; for (int i = 1; i <= n; ++i) { if (v<mi) mi = v; mil[i] = mi;//计算从1->i过程中出现的最小油量(可能<0) v += a[i]; v -= b[i]; } vr[n+1] = mir[n+1] = 0;//n+1就等价于1这个点 for (int i = n; i >= 1; --i) { v = a[i]; v -= b[i]; vr[i] = vr[i+1] + v;//i 到达 1后剩余油量 mir[i] = min(mir[i+1]+v, v);//计算从i->1过程中出现的最小油量 i->1的最小油量= (i+1)->1的最小油量 + v } dep = 0; for (int i = 1; i <= n; ++i) if (mir[i] >= 0) if (mil[i] + vr[i] >= 0) {//有了剩余油量就相当于全程的最小值都+vr[i] ,这样就能得到全程最小值 ans[dep++] = i; } printf("Case %d:", ++T); for (int i = 0; i < dep; ++i) printf(" %d", ans[i]); puts(""); } int main() { while (~scanf("%d", &n), n) work(); return 0; }
UVALive 6088 | Approximate Sorting |
题解:点击打开链接
UVALive 6089 | Nine |
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <bits/stdc++.h> template <class T> inline bool rd(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c<'0' || c>'9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } template <class T> inline void pt(T x) { if (x <0) { putchar('-'); x = -x; } if (x>9) pt(x / 10); putchar(x % 10 + '0'); } using namespace std; typedef long long ll; const int N = 505; struct node{ int jiu, cha, x, y; node(int a=0,int b=0,int c=0,int d=0):jiu(a),cha(b),x(c),y(d){} }ans; node work(node X, node Y){ bool hehe; if(X.jiu != Y.jiu){ hehe = X.jiu > Y.jiu; } else if(X.cha != Y.cha){ hehe = X.cha < Y.cha; } else if(X.x != Y.x){ hehe = X.x < Y.x; } else hehe = X.y < Y.y; return hehe ? X : Y; } int Find(int a, int b){ int siz = 0; while(a){ siz += (a%10)==9; a/=10; } while(b){ siz += (b%10)==9; b/=10; } return siz; } int h, s, all; int C(int a, int b){ int siz = a*60+b; siz = all - siz; if(siz < 0) siz = -siz; return siz; } int main(){ while (~scanf("%d:%d", &h,&s), h+s){ all = h*60+s; ans = node(Find(h,s), 0, h, s); for(int i = 0; i < 100; i++) for(int j = 0; j < 100; j++) { node tmp = node(Find(i,j), C(i,j), i, j); if(tmp.cha * 10 < all) { ans = work(ans, tmp); } } printf("%02d:%02d\n", ans.x, ans.y); } return 0; }
UVALive 6091 | Trees |
题解:点击打开链接
UVALive 6092 | Catching Shade in Flatland |
题解:点击打开链接