1053 Path of Equal Weight (30 分)
vector存储答案,排序,最后倒序输出就好
注意只有一个结点(即根节点)的情况
#include<bits/stdc++.h>
using namespace std;
vector<int> v[105];
int wei[105], s, cnt, isr[105];
vector<int> tmp;
vector<int> st[1005];
void dfs(int now, int w) {
if(w > s) return;
if(w == s) {
if(isr[now]) st[cnt++] = tmp;
return;
}
for(int i = 0; i < v[now].size(); i++) {
int y = v[now][i];
if(v[y].size() == 0) isr[y] = 1;
tmp.push_back(wei[y]);
dfs(y, w + wei[y]);
tmp.pop_back();
}
}
int main() {
int n, m, p, k, kid;
scanf("%d %d %d",&n, &m, &s);
for(int i = 0; i < n; i++) scanf("%d", wei+i);
for(int i = 0; i < m; i++) {
scanf("%d %d",&p, &k);
while(k--) {
scanf("%d",&kid);
v[p].push_back(kid);
}
}
if(n == 1) isr[0] = 1;
tmp.push_back(wei[0]);
dfs(0, wei[0]);
sort(st, st+cnt);
for(int j = cnt - 1; j >= 0; j--) {
for(int i = 0; i < st[j].size(); i++) {
if(i) printf(" ");
printf("%d",st[j][i]);
}
printf("\n");
}
return 0;
}
/*
20 9 24
10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2
00 4 01 02 03 04
02 1 05
04 2 06 07
03 3 11 12 13
06 1 09
07 2 08 10
16 1 15
13 3 14 16 17
17 2 18 19
1 0 10
10
*/
1054 The Dominant Color (20 分)
无聊的把n和m掉了个,其他无任何需要注意的点
#include<bits/stdc++.h>
using namespace std;
map<int,int> mp;
int main() {
int n, m, ma, co;
scanf("%d %d",&m,&n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int a;
scanf("%d",&a);
mp[a]++;
if(mp[a] > ma) {
ma = mp[a];
co = a;
}
}
}
printf("%d\n", co);
return 0;
}
1055 The World’s Richest (25 分)
这题真的很无聊,就一个排序罢了,简单提一下,结构体数组排序会超时,set也会超时,set可以理解,内部实现是红黑树,但是vector就不会了,就可能还需要看看源码吧,但是我不想管了,记住就行了,还有一个限制年龄处理的方法,第1组数据不会超时,第2组数据超时,真的奇怪。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct node{
int age, w;
string name;
friend bool operator <(node a, node b) {
if(a.w == b.w) {
if(a.age == b.age) return a.name < b.name;
else return a.age < b.age;
}
else return a.w > b.w;
}
};
vector<node> v;
int main() {
int n, q;
scanf("%d %d",&n,&q);
node temp;
temp.name.resize(10);
for(int i = 0; i < n; i++) {
scanf("%s %d %d",&temp.name[0],&temp.age,&temp.w);
v.push_back(temp);
}
sort(v.begin(), v.end());
for(int i = 0; i < q; i++) {
int m, tm, amin, amax;
scanf("%d %d %d",&m,&amin,&amax);
tm = m;
printf("Case #%d:\n",i+1);
for(int j = 0; j < n; j++) {
if(!m) break;
if(v[j].age >= amin && v[j].age <= amax) {
printf("%s %d %d\n",v[j].name.c_str(),v[j].age,v[j].w);
m--;
}
}
if(m == tm)
printf("None\n");
}
return 0;
}
超时代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct node{
int age, w;
string name;
friend bool operator <(node a, node b) {
if(a.w == b.w) {
if(a.age == b.age) return a.name < b.name;
else return a.age < b.age;
}
else return a.w > b.w;
}
}per[N];
vector<int> k[205];
int main() {
int n, q;
scanf("%d %d",&n,&q);
for(int i = 0; i < n; i++) {
cin >> per[i].name >> per[i].age >> per[i].w;
k[per[i].age].push_back(i);
}
for(int i = 0; i < q; i++) {
int m, amin, amax;
cin >> m >> amin >> amax;
set<node> ans;
for(int j = amin; j <= amax; j++) {
int len = k[j].size();
for(int p = 0; p < len; p++) {
int ind = k[j][p];
ans.insert(per[ind]);
// cout<<per[ind].name<<" "<<per[ind].age<<" "<<per[ind].w<<endl;
}
}
printf("Case #%d:\n",i+1);
if(ans.size()) {
for(set<node>::iterator it = ans.begin(); it != ans.end(); it++){
if(!m--) break;
cout<<(*it).name<<" "<<(*it).age<<" "<<(*it).w<<endl;
}
}
else printf("None\n");
}
return 0;
}
1056 Mice and Rice (25 分)
我写的很复杂,先模拟比赛过程,把每个人是第几轮淘汰的都记录下来并且放进一个栈里,最后再一一出栈并获得真实的排名,最后输出即可
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int rk[N], w[N];
vector<int> player;
stack<pair<int, int> >q;
int main() {
int n, k, t;
scanf("%d %d",&n, &k);
for(int i = 0; i < n; i++) {
scanf("%d",w+i);
}
for(int i = 0; i < n; i++) {
scanf("%d",&t);
player.push_back(t);
}
int turn = 0, tot = log(n)/log(k) + 2;
while(player.size() > 1) {
turn++;
int i = 0;
vector<int> winner;
while(i < player.size()) {
int ma = -1, p;
for(int j = 0; j < k && i < player.size(); j++, i++) {
int y = player[i];
rk[y] = turn;
if(w[y] > ma) {
if(ma != -1) q.push(make_pair(p, rk[p]));
ma = w[y];
p = y;
}
else q.push(make_pair(y, rk[y]));
}
winner.push_back(p);
}
player = winner;
}
q.push(make_pair(player[0], turn+1));
int rank = 0, lazy = 1, pre = -1;
for(int i = 0; i < n; i++) {
int y = q.top().first, now = q.top().second;
q.pop();
if(pre == now) lazy++;
else {
rank+=lazy;
lazy = 1;
}
rk[y] = rank;
pre = now;
}
for(int i = 0; i < n; i++) {
if(i) printf(" ");
printf("%d", rk[i]);
}
return 0;
}
1057 Stack (30 分)
树状数组维护,并且用二分查询第k大
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int a[N], c[N];
stack<int> st;
int ask(int x) {
int sum = 0;
for(int i = x; i; i -= i & -i) {
sum += c[i];
}
return sum;
}
void add(int x, int v) {
for(int i = x; i <= N; i += i & -i) {
c[i] += v;
}
}
int query(int k) {
int l = 1, r = N, mid;
while(l <= r) {
mid = (l + r) / 2;
if(ask(mid) >= k) r = mid - 1;
else l = mid + 1;
}
return l;
}
int main() {
int n;
scanf("%d", &n);
while(n--) {
string s;
int t;
cin >> s;
if(s == "Push") {
scanf("%d",&t);
st.push(t);
add(t, 1);
}
else if(s == "Pop") {
if(st.empty()) printf("Invalid\n");
else{
t = st.top();
st.pop();
add(t, -1);
printf("%d\n", t);
}
}
else{
if(st.empty()) printf("Invalid\n");
else{
t = query((st.size() + 1) / 2);
printf("%d\n", t);
}
}
}
return 0;
}
1058 A+B in Hogwarts (20 分)
简单数字处理,跟大整数计算时候的处理类似,记录进位即可
#include<bits/stdc++.h>
using namespace std;
int main() {
int g1, s1, k1, g2, s2, k2, g, s, k;
scanf("%d.%d.%d",&g1,&s1,&k1);
scanf("%d.%d.%d",&g2,&s2,&k2);
int sum = k1 + k2;
int p = sum / 29;
k = sum % 29;
sum = p + s1 + s2;
p = sum / 17;
s = sum % 17;
g = g1 + g2 + p;
printf("%d.%d.%d",g,s,k);
return 0;
}
1059 Prime Factors (25 分)
模板题,测试点3是n=1时候的情况
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int p[105], c[105];
int main() {
int n, t, m = 0;
scanf("%d",&n);
t = n;
for(int i = 2; i <= sqrt(n); i++) {
if(n % i == 0) {
p[++m] = i, c[m] = 0;
while(n % i == 0) n /= i, c[m]++;
}
}
if(n > 1) {
p[++m] = n, c[m] = 1;
}
printf("%d=",t);
if(t == 1) printf("1");
else {
for(int i = 1; i <= m; i++) {
if(i != 1) printf("*");
if(c[i] != 1) {
printf("%d^%d",p[i], c[i]);
}
else printf("%d",p[i]);
}
}
return 0;
}
1060 Are They Equal (25 分)
有一些小坑点,比如前置0,0.123, 0.0123这种数的存储要注意(第3个测试点),还有12345和0.01234是相等的,我一开始判断数位最后一个测试点会错,但不判断了就对了
#include<bits/stdc++.h>
using namespace std;
int n, d1[105], d2[105], f = 0;
struct number{
int in[105], l1;//整数部分
int fra[105], l2;//小数部分
number(){
l1 = l2 = 0;
memset(in, 0, sizeof(in));
memset(fra, 0, sizeof(fra));
}
};
number init(string s) {//考虑是否有前置0的情况,包括0.123这样的情况,处理成.123
int l = s.length();
number t;
int i;
for(i = 0; i < l; i++) {
if(s[i] != '0') break;
}
for(; i < l; i++) {
if(s[i] == '.') {
i++;
break;
}
t.in[t.l1++] = s[i] - '0';
}
if(t.l1 == 0) {
for(; i < l; i++) {
if(s[i] != '0') break;
t.l1--;
}
}
for(; i < l; i++) {
t.fra[t.l2++] = s[i] - '0';
}
return t;
}
int main() {
string s1, s2;
cin >> n >> s1 >> s2;
number n1 = init(s1);
number n2 = init(s2);
// if(n1.l1 != n2.l1) f = 1;
int i, j;
for(i = 0, j = 0; j < n1.l1 && i < n; i++, j++) d1[i] = n1.in[j];
for(j = 0; i < n && j < n1.l2; i++, j++) d1[i] = n1.fra[j];
for(; i < n; i++) d1[i] = 0;
for(i = 0, j = 0; j < n2.l1 && i < n; i++, j++) {
d2[i] = n2.in[j];
if(d2[i] != d1[i]) f = 1;
}
for(j = 0; i < n && j < n2.l2; i++, j++) {
d2[i] = n2.fra[j];
if(d2[i] != d1[i]) f = 1;
}
for(; i < n; i++) {
d2[i] = 0;
if(d2[i] != d1[i]) f = 1;
}
if(f) {
printf("NO ");
printf("0.");
for(i = 0; i < n; i++) printf("%d",d1[i]);
printf("*10^%d ",n1.l1);
printf("0.");
for(i = 0; i < n; i++) printf("%d",d2[i]);
printf("*10^%d",n2.l1);
}
else {
printf("YES ");
printf("0.");
for(i = 0; i < n; i++) printf("%d",d1[i]);
printf("*10^%d",n1.l1);
}
return 0;
}