pat1081-1090
1081
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 105;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)
struct Node{
ll nu, di;
Node(ll a=0, ll b=0):nu(a), di(b){}
}E[N];
ll gcd(ll x, ll y) {
if(y == 0) return x;
else return gcd(y, x%y);
}
ll Abs(ll x) {
if(x < 0) return -x;
else return x;
}
int main() {
int n;
while(~scanf("%d", &n)) {
for(int i = 0; i < n; ++i) {
char s[100];
int positive = 1;
scanf("%s", s);
if(s[0] == '-') {
positive *= -1;
}
ll t1 = 0; ll t2 = 0;
int j;
for(j = 0; s[j]; ++j) {
if(s[j] == '/') break;
else if(s[j] != '-') t1 = t1*10 + s[j]-'0';
}
for(j++; s[j]; ++j) {
t2 = t2*10 + s[j]-'0';
}
ll tmp = gcd(t1, t2);
t1 /= tmp; t2 /= tmp;
E[i] = Node(t1*positive, t2);
// printf("%d %d\n", t1, t2);
}
Node nw = Node(E[0].nu, E[0].di);
for(int i = 1; i < n; ++i) {
ll t2 = 1ll* E[i].di * nw.di / gcd(E[i].di, nw.di);
ll t1 = nw.nu * t2 / nw.di + E[i].nu * t2 / E[i].di;
int positive = 1;
if(t1 < 0) positive *= -1;
if(t2 < 0) positive *= -1;
t1 = Abs(t1); t2 = Abs(t2);
ll tmp = gcd(t1, t2);
t1 /= tmp; t2 /= tmp;
nw = Node(t1*positive, t2);
}
if(nw.nu % nw.di == 0) printf("%lld\n", nw.nu/nw.di);
else {
if(nw.nu / nw.di) printf("%lld ", nw.nu / nw.di);
printf("%lld/%lld\n", nw.nu%nw.di, nw.di);
}
}
return 0;
}
1082 挺难一个大模拟,我是把亿,万,分别处理的
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 105;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)
const int e8 = 1e8;
const int e4 = 1e4;
struct Node{
int num[5];
}E[4];
char A[12][10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
char B[5][10] = {"Shi","Bai", "Qian", "Wan", "Yi"};
vector<int> ans;
void solve(Node &a, int x) {
int cnt = 0;
while(x) {
a.num[cnt++] = x%10;
x /= 10;
}
}
void print(Node &a) {
// for(int i = 0; i < 4; ++i) printf("%d ", a.num[i]); printf("\n");
if(a.num[3] == 0 && ans.size() > 0) ans.push_back(0);
if(a.num[3]) ans.push_back(a.num[3]), ans.push_back(12);
if(a.num[2]) ans.push_back(a.num[2]), ans.push_back(11);
if(a.num[1] == 0 && a.num[0] && a.num[2]) ans.push_back(0);
if(a.num[3] && a.num[1] && a.num[2] == 0) ans.push_back(0);
if(a.num[2] == 0 && a.num[1] == 0 && a.num[0] && a.num[3]) ans.push_back(0);
if(a.num[1]) ans.push_back(a.num[1]), ans.push_back(10);
if(a.num[0]) ans.push_back(a.num[0]);
}
int main() {
int n;
while(~scanf("%d", &n)) {
if(n == 0) {
printf("ling\n"); continue;
}
ans.clear();
for(int i = 0; i <= 3; ++i) {
memset(E[i].num, 0, sizeof(E[i].num));
}
if(n < 0) {
n = -n;
printf("Fu ");
}
solve(E[1], n/e8); solve(E[2], (n%e8)/e4); solve(E[3], n%e4);
int pre = ans.size();
print(E[1]);
if(ans.size() > pre) {
ans.push_back(14);
pre = ans.size();
}
print(E[2]);
if(ans.size() > pre) {
ans.push_back(13);
pre = ans.size();
}
print(E[3]);
for(int i = 0; i < ans.size(); ++i) {
if(i) printf(" ");
if(ans[i] < 10) printf("%s", A[ans[i]]);
else printf("%s", B[ans[i]%10]);
}
printf("\n");
}
return 0;
}
1083
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 105;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)
struct Node{
char nam[15];
char Id[15];
int grade;
bool operator < (const Node & T) const {
return grade > T.grade;
}
}E[N];
int main() {
int n;
while(~scanf("%d", &n)) {
for(int i = 0; i < n; ++i) {
scanf("%s %s %d", E[i].nam, E[i].Id, &E[i].grade);
}
int g1, g2;
scanf("%d %d", &g1, &g2);
if(g1 > g2) swap(g1, g2);
sort(E, E+n);
int has = 0;
for(int i = 0; i < n; ++i) {
if(E[i].grade >= g1 && E[i].grade <= g2) {
printf("%s %s\n", E[i].nam, E[i].Id);
has ++;
}
}
if(!has) printf("NONE\n");
}
return 0;
}
1084
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 105;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)
char s1[85], s2[85];
int asci[128];
int main() {
while(~scanf("%s %s", s1, s2)) {
for(int i = 0; i < strlen(s2); ++i) {
if(s2[i] >= 'a' && s2[i] <= 'z') s2[i] = s2[i]-'a'+'A';
asci[s2[i]] ++;
}
for(int i = 0; i < strlen(s1); ++i) {
if(s1[i] >= 'a' && s1[i] <= 'z') s1[i] = s1[i]-'a'+'A';
if(!asci[s1[i]]) {
asci[s1[i]] ++;
printf("%c", s1[i]);
}
}
printf("\n");
}
return 0;
}
1085
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)
int A[N];
int main() {
int n, p;
while(~scanf("%d %d", &n, &p)) {
for(int i = 0; i < n; ++i) scanf("%d", &A[i]);
if(p < 1) {
printf("0\n"); continue;
}
sort(A, A+n);
int j = 0;
int ans = -1;
for(int i = 0; i < n; ++i) {
for(j = max(j, i); j < n; ++j) {
if(A[j] > 1ll*A[i] *p) break;
else ans = max(ans, j-i+1);
}
}
printf("%d\n", ans);
}
return 0;
}
1086 需要思考下,其实可以直接建图
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)
int L[35]; int R[35]; int fa[35];
int root;
int fl;
void dfs(int x) {
if(L[x]) dfs(L[x]);
if(R[x]) dfs(R[x]);
if(!fl) fl = 1;
else printf(" ");
printf("%d", x);
}
int main() {
int n;
while(~scanf("%d", &n)) {
memset(L, 0, sizeof(L));
memset(R, 0, sizeof(R));
fl = 0;
stack<int> st;
root = 0;
int pre = -INF;
for(int i = 0; i < 2*n; ++i) {
// printf("%d\n", nw);
char s[10];
scanf("%s", s);
if(s[1] == 'u') {
int a; scanf("%d", &a);
st.push(a);
if(pre == -INF) root = a;
else if(pre < 0) {
R[-pre] = a;
}else L[pre] = a;
pre = a;
}else {
pre = -st.top();
st.pop();
}
}
dfs(root);
printf("\n");
}
return 0;
}
1087
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 205;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)
map<string, int> mp;
int tot;
char nam[205][10];
int hap[205];
int Start = 1, End;
struct Node{
int nx, to, di;
}E[N*N*2];
int head[N], tol;
void add(int fr, int to, int di) {
E[tot].to = to; E[tot].di = di; E[tot].nx = head[fr]; head[fr] = tot++;
}
struct Hode{
int po, di;
Hode(int a=0, int b=0):po(a), di(b){}
bool operator < (const Hode &T) const {
return di > T.di;
}
};
int vis[N], dis[N];
void dij(int x) {
memset(vis, 0, sizeof(vis));
memset(dis, INF, sizeof(dis));
dis[x] = 0;
priority_queue<Hode> Q;
Q.push(Hode(x, dis[x]));
while(!Q.empty()) {
int po = Q.top().po; Q.pop();
if(vis[po]) continue;
vis[po] = 1;
for(int i = head[po]; ~i; i = E[i].nx) {
int to = E[i].to;
if(dis[to] > dis[po] + E[i].di) {
dis[to] = dis[po] + E[i].di;
Q.push(Hode(to, dis[to]));
}
}
}
}
vector<int> ans; int Ans;
vector<int> tmp; int Tmp;
int all = 0;
void dfs(int x) {
if(x == End) {
all ++;
// printf("%d\n", Tmp);
if(Tmp > Ans) {
ans.clear();
for(int i = 0; i < tmp.size(); ++i) ans.push_back(tmp[i]); Ans = Tmp;
}else if(Tmp == Ans && tmp.size() < ans.size()) {
ans.clear();
for(int i = 0; i < tmp.size(); ++i) ans.push_back(tmp[i]);
}
return;
}
for(int i = head[x]; ~i; i = E[i].nx) {
int to = E[i].to;
if(dis[to] == dis[x] + E[i].di) {
tmp.push_back(to); Tmp += hap[to];
dfs(to);
tmp.erase(--tmp.end()); Tmp -= hap[to];
}
}
}
int main() {
int n, k;
char s[10];
while(~scanf("%d %d %s", &n, &k, s)) {
all = 0;
tot = 0; Ans = -1;
memset(head, -1, sizeof(head));
tol = 0;
if(mp.find(s) == mp.end()) {
mp[s] = ++tol;
// printf("%s\n",s);
for(int j = 0; j <= strlen(s); ++j) nam[tol][j] = s[j];
// printf("%s\n", nam[tol]);
}
for(int i = 2; i <= n; ++i) {
int a; scanf("%s %d", s, &a);
if(strcmp(s, "ROM") == 0) {
End = i;
}
if(mp.find(s) == mp.end()) {
mp[s] = ++tol;
for(int j = 0; j <= strlen(s); ++j) nam[tol][j] = s[j];
}
hap[tol] = a;
}
// printf("%d\n", tol);
for(int i = 0; i < k; ++i) {
char a[10]; char b[10]; int c;
scanf("%s %s %d", a, b, &c);
add(mp[a], mp[b], c); add(mp[b], mp[a], c);
}
dij(Start);
// printf("%d %d\n", Start, End);
Tmp = 0; tmp.clear();
dfs(Start);
printf("%d %d %d %d\n%s", all, dis[End], Ans, Ans/ans.size(), nam[1]);
for(int i = 0; i < ans.size(); ++i) {
printf("->%s", nam[ans[i]]);
}
printf("\n");
}
return 0;
}
1088这题查了很长时间,其实就是一个地方 数字越界了!,应该多思考,思考会错误的地方
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 205;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)
char s[] = "+-*/";
ll gcd(ll a, ll b) {
if(b == 0) return a;
else return gcd(b, a%b);
}
ll Abs(ll x) {
if(x < 0) return -x;
else return x;
}
void doo(ll a, ll b) {
// if(b> 1e10) while(1);
// printf(" hh%lld:%lld ", a, b);
if(b == 0) { printf("Inf"); return; }
if(a == 0) { printf("0"); return; }
int fl = 1;
if(a * b < 0) {
fl = -1;
}
a = Abs(a); b = Abs(b);
ll tmp = gcd(a, b);
a /= tmp; b /= tmp;
if(fl == -1) printf("(-");
if(b == 1) {
printf("%lld", a);
}else {
if(a/b == 0) printf("%lld/%lld", a, b);
else printf("%lld %lld/%lld", a/b, a%b, b);
}
if(fl == -1) printf(")");
}
void show(ll a, ll b, ll c, ll d, ll e, ll f, int ty) {
doo(a, b);
printf(" %c ", s[ty-1]);
doo(c, d);
printf(" = ");
doo(e, f);
printf("\n");
}
int main() {
ll a,b,c,d;
while(~scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d)) {
if(a < 0 && b < 0) {
a = -a; b = -b;
}
if(a > 0 && b < 0) {
a = -a; b = -b;
}
if(c < 0 && d < 0) {
c = -c; d = -d;
}
if(c > 0 && d < 0) {
c = -c; d = -d;
}
if(a) {
ll tt = gcd( Abs(a), Abs(b) );
a /= tt; b /= tt;
}
if(c) {
ll tt = gcd( Abs(c), Abs(d) );
c /= tt; d /= tt;
}
// printf("%lld\n", c);
//add
show(a, b, c, d, a*d+b*c, b*d, 1);
//minus
show(a, b, c, d, a*d-b*c, b*d, 2);
//mul
// if(tmp > 1e9) while(1);
show(a, b, c, d, a*c, b*d, 3);
//dul
show(a, b, c, d, a*d, c*b, 4);
}
return 0;
}
1089 考察归并排序的一题,还是那句话,暑假得复习数据结构啦
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 205;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)
int init[N];
int temp[N];
int a[N];
int n;
void insert(int x) {
int po = x; int tmp = a[x];
for(int j = 1; j < x; ++j) {
if(a[x] <= a[j]) {
po = j;
break;
}
}
for(int j = x; j >= po+1; --j) {
a[j] = a[j-1];
}
a[po] = tmp;
}
void Merge(int x, int ty) {
for(int i = 1; i <= n; i += x) {
int t1 = i; int t2 = min(n, i+x-1);
sort(a+t1, a+t2+1);
}
if(!ty) {
printf("Merge Sort\n");
for(int i = 1; i <= n; ++i) {
if(i != 1) printf(" ");
printf("%d", a[i]);
}
printf("\n");
return;
}
int fl = 1;
for(int i = 1; i <= n; ++i) {
if(a[i] != temp[i]) {
fl = 0; break;
}
}
if(fl) Merge(x*2, 0);
else Merge(x*2, ty);
}
int main() {
while(~scanf("%d", &n)) {
for(int i = 1; i <= n; ++i) scanf("%d", &init[i]);
for(int i = 1; i <= n; ++i) scanf("%d", &temp[i]);
for(int i = 1; i <= n; ++i) a[i] = init[i];
int ju = 0;
for(int i = 1; i <= n; ++i) {
insert(i);
// for(int j = 1; j <= n; ++j) printf("%d ", a[j]); printf("\n");
int nn = 1;
for(int j = 1; j <= n; ++j) {
if(temp[j] != a[j]) {
nn = 0; break;
}
}
int mx = 1;
for(int j = 2; j <= n; ++j) {
if(temp[j] > temp[j-1]) {
mx = j;
}else break;
}
if(mx > i) continue;
if(nn) {
printf("Insertion Sort\n");
ju = 1;
insert(i+1);
for(int j = 1; j <= n; ++j) {
if(j != 1) printf(" ");
printf("%d", a[j]);
} printf("\n");
break;
}
}
// ju = 0;
if(!ju) {
for(int i = 1; i <= n; ++i) a[i] = init[i];
Merge(2, 1);
// while(1);
}
}
return 0;
}
1090
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+5;
typedef long long ll;
#define MP(x, y) make_pair(x, y)
int n; double p, r;
struct Node{
int to, nx;
}E[N*1000];
int head[N]; int tot;
void add(int fr, int to) {
E[tot].to = to; E[tot].nx = head[fr]; head[fr] = tot++;
}
int has[N];
double ans;
vector<double> vc;
void dfs(int x, double price) {
if(!has[x]) {
if(ans < price) {
ans = price;
}
vc.push_back(price);
return;
}
for(int i = head[x]; ~i; i = E[i].nx) {
int to = E[i].to;
dfs(to, price*(1+ r/100));
}
}
int main() {
while(~scanf("%d %lf %lf", &n, &p, &r)) {
ans = -1;
memset(head, -1, sizeof(head));
memset(has, 0, sizeof(has));
tot = 0; ans = 0;
int root;
for(int i = 0; i < n; ++i) {
int a; scanf("%d", &a);
if(a == -1) root = i;
else add(a, i), has[a] ++;
}
dfs(root, p);
int all = 0;
for(int i = 0; i < vc.size(); ++i) {
if(vc[i] == ans) {
all ++;
}
}
printf("%.2f %d\n", ans , all);
}
return 0;
}