目录
1069 The Black Hole of Numbers
1008 Elevator
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
int res = 0;
int last = 0;
while (n -- ){
int cur;
cin >> cur;
if (last < cur) res += (cur - last) * 6;
else res += (last - cur) * 4;
res += 5;
last = cur;
}
cout << res << endl;
return 0;
}
1011 World Cup Betting
解析
注意点
保留两位小数:printf(" %.2lf ",x)
#include <iostream>
using namespace std;
int main(){
double res = 1;
for (int i = 0; i < 3; i ++ ){
double w, t, l;
cin >> w >> t >> l;
double x = max(w, max(t, l)); //三个数比较大小
if (x == w) cout << "W ";
else if (x == t) cout << "T ";
else cout << "L ";
res *= x;
}
printf("%.2lf\n", (res * 0.65 - 1) * 2);
return 0;
}
1014 Waiting in Line
解析
注意点
#include <iostream>
#include <cstring>
#include <queue>
#include <unordered_map>
using namespace std;
const int N = 20;
int n, m, k, Q;
int sum[N];
queue<int> q[N];
int main(){
cin >> n >> m >> k >> Q;
unordered_map<int, int> hash;
for (int i = 1; i <= k; i ++ ){
int s;
cin >> s;
int t = 0;
for (int j = 0; j < n; j ++ )
if (i <= n * m){
if (q[j].size() < q[t].size()) t = j;
}
else{
if (q[j].front() < q[t].front()) t = j;
}
sum[t] += s;
if (i > n * m) q[t].pop();
q[t].push(sum[t]);
if (sum[t] - s < 540) hash[i] = sum[t];
}
while (Q -- ){
int id;
cin >> id;
if (hash.count(id)) printf("%02d:%02d\n", hash[id] / 60 + 8, hash[id] % 60);
else puts("Sorry");
}
return 0;
}
1031 Hello World for U
解析
注意点
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100;
char g[N][N];
int main(){
string str;
cin >> str;
int n = str.size();
int n1 = (n + 2) / 3;
int n2 = n + 2 - n1 * 2;
int k = 0;
for (int i = 0; i < n1; i ++ ) g[i][0] = str[k ++ ];
for (int i = 1; i < n2; i ++ ) g[n1 - 1][i] = str[k ++ ];
for (int i = n1 - 2; i >= 0; i -- ) g[i][n2 - 1] = str[k ++ ];
for (int i = 0; i < n1; i ++ ){
for (int j = 0; j < n2; j ++ )
if (g[i][j]) cout << g[i][j];
else cout << ' ';
cout << endl;
}
return 0;
}
1041 Be Unique
解析
注意点
#include <iostream>
using namespace std;
const int N = 100010;
int n;
int a[N], c[N];
int main(){
cin >> n;
for (int i = 0; i < n; i ++ ){
cin >> a[i];
c[a[i]] ++ ;
}
for (int i = 0; i < n; i ++ )
if (c[a[i]] == 1){
cout << a[i] << endl;
return 0;
}
puts("None");
return 0;
}
1042 Shuffling Machine
解析
序列置换
#include <iostream>
#include <cstring>
using namespace std;
const int N = 60;
int k;
int q[N], p[N], w[N]; //q存打乱的顺序 p存结果
void print(int x){
if (x <= 13) cout << 'S' << x;
else if (x <= 26) cout << 'H' << x - 13;
else if (x <= 39) cout << 'C' << x - 26;
else if (x <= 52) cout << 'D' << x - 39;
else cout << 'J' << x - 52;
}
int main(){
cin >> k;
for (int i = 1; i <= 54; i ++ ) cin >> q[i];
for (int i = 1; i <= 54; i ++ ) p[i] = i;
while (k -- ){
memcpy(w, p, sizeof w); //把p复制一份到w里
for (int i = 1; i <= 54; i ++ ) p[q[i]] = w[i];
}
for (int i = 1; i <= 54; i ++ ){
print(p[i]);
if (i != 54) cout << ' ';
}
return 0;
}
1047 Student List for Course
解析
注意点
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
vector<string> curse[n+1];
for(int i=0;i<n;i++)
{
char tem[5];
int p; scanf("%s%d",tem,&p);
string name(tem);
for(int i=0;i<p;i++)
{
int lei;
scanf("%d",&lei);
curse[lei].push_back(name);
}
}
for(int i=1;i<=k;i++)
{
printf("%d %d\n",i,curse[i].size());
sort(curse[i].begin(),curse[i].end());
for(auto x:curse[i])
printf("%s\n", (x).c_str());
}
return 0;
}
1054 The Dominant Color
解析
注意点
#include <iostream>
#include <unordered_map>
using namespace std;
int main(){
int n, m;
scanf("%d%d", &n, &m);
unordered_map<int, int> cnt;
for (int i = 0; i < n * m; i ++ ){
int x;
scanf("%d", &x);
if ( ++ cnt[x] > n * m / 2){
printf("%d\n", x);
break;
}
}
return 0;
}
1056 Mice and Rice
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1010;
int n, m;
int w[N], ans[N];
int main(){
cin >> n >> m;
for (int i = 0; i < n; i ++ ) cin >> w[i];
vector<int> cur(n);
for (int i = 0; i < n; i ++ ) cin >> cur[i];
while (cur.size() > 1){
vector<int> next;
int remain = (cur.size() + m - 1) / m;
for (int i = 0; i < cur.size();){
int j = min((int)cur.size(), i + m);
int t = i;
for (int k = i; k < j; k ++ )
if (w[cur[k]] > w[cur[t]])
t = k;
next.push_back(cur[t]);
for (int k = i; k < j; k ++ )
if (k != t)
ans[cur[k]] = remain + 1;
i = j;
}
cur = next;
}
ans[cur[0]] = 1;
cout << ans[0];
for (int i = 1; i < n; i ++ ) cout << ' ' << ans[i];
cout << endl;
return 0;
}
1062 Talent and Virtue
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, L, H;
struct Person
{
int id, moral, talent;
int level;
int total() const
{
return moral + talent;
}
bool operator< (const Person &t) const
{
if (level != t.level) return level < t.level;
if (total() != t.total()) return total() > t.total();
if (moral != t.moral) return moral > t.moral;
return id < t.id;
}
}p[N];
int main()
{
scanf("%d%d%d", &n, &L, &H);
int m = 0;
for (int i = 0; i < n; i ++ )
{
int id, moral, talent;
scanf("%d%d%d", &id, &moral, &talent);
if (moral < L || talent < L) continue;
int level;
if (moral >= H && talent >= H) level = 1;
else if (moral >= H && talent < H) level = 2;
else if (moral < H && talent < H && moral >= talent) level = 3;
else level = 4;
p[m ++ ] = {id, moral, talent, level};
}
sort(p, p + m);
printf("%d\n", m);
for (int i = 0; i < m; i ++ )
printf("%08d %d %d\n", p[i].id, p[i].moral, p[i].talent);
return 0;
}
1065 A+B and C (64bit)
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
bool check(LL a, LL b, LL c)
{
LL d = a + b;
if (a >= 0 && b >= 0 && d < 0) return true;
if (a < 0 && b < 0 && d >= 0) return false;
return a + b > c;
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i ++ )
{
LL a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
if (check(a, b, c)) printf("Case #%d: true\n", i);
else printf("Case #%d: false\n", i);
}
return 0;
}
1069 The Black Hole of Numbers
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> get(int n)
{
int nums[4];
for (int i = 0; i < 4; i ++ )
{
nums[i] = n % 10;
n /= 10;
}
sort(nums, nums + 4);
int a = 0;
for (int i = 0; i < 4; i ++ ) a = a * 10 + nums[i];
reverse(nums, nums + 4);
int b = 0;
for (int i = 0; i < 4; i ++ ) b = b * 10 + nums[i];
return {b, a};
}
int main()
{
int n;
cin >> n;
do
{
auto t = get(n);
printf("%04d - %04d = %04d\n", t[0], t[1], t[0] - t[1]);
n = t[0] - t[1];
} while (n && n != 6174);
return 0;
}
1080 Graduate Admission
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 40010, M = 110, K = 5;
int n, m, k;
int cnt[N];
int wish[N];
vector<int> uty[M];
struct Person
{
int id, ge, gi;
int wish[K];
int total() const
{
return ge + gi;
}
bool operator< (const Person &t) const
{
if (total() != t.total()) return total() > t.total();
return ge > t.ge;
}
bool operator== (const Person &t) const
{
return ge == t.ge && gi == t.gi;
}
}p[N];
int main()
{
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; i ++ ) scanf("%d", &cnt[i]);
for (int i = 0; i < n; i ++ )
{
p[i].id = i;
scanf("%d%d", &p[i].ge, &p[i].gi);
for (int j = 0; j < k; j ++ )
scanf("%d", &p[i].wish[j]);
}
sort(p, p + n);
memset(wish, -1, sizeof wish);
for (int i = 0; i < n;)
{
int j = i + 1;
while (j < n && p[i] == p[j]) j ++ ;
for (int t = i; t < j; t ++ )
for (int u = 0; u < k; u ++ )
{
int w = p[t].wish[u];
if (cnt[w] > uty[w].size())
{
wish[t] = w;
break;
}
}
for (int t = i; t < j; t ++ )
if (wish[t] != -1)
uty[wish[t]].push_back(p[t].id);
i = j;
}
for (int i = 0; i < m; i ++ )
{
if (uty[i].size())
{
sort(uty[i].begin(), uty[i].end());
printf("%d", uty[i][0]);
for (int j = 1; j < uty[i].size(); j ++ )
printf(" %d", uty[i][j]);
}
puts("");
}
return 0;
}
1083 List Grades
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n;
struct Person
{
string name, id;
int grade;
bool operator< (const Person &t) const
{
return grade > t.grade;
}
}p[N];
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> p[i].name >> p[i].id >> p[i].grade;
int g1, g2;
cin >> g1 >> g2;
int m = 0;
for (int i = 0; i < n; i ++ )
if (p[i].grade >= g1 && p[i].grade <= g2)
p[m ++ ] = p[i];
if (!m) puts("NONE");
else
{
sort(p, p + m);
for (int i = 0; i < m; i ++ )
cout << p[i].name << ' ' << p[i].id << endl;
}
return 0;
}
1092 To Buy or Not to Buy
#include <iostream>
#include <cstring>
#include <unordered_map>
using namespace std;
int main()
{
string a, b;
cin >> a >> b;
unordered_map<char, int> S;
for (auto c : a) S[c] ++ ;
for (auto c : b) S[c] -- ;
int sp = 0, sn = 0;
for (auto item : S)
if (item.second > 0) sp += item.second;
else sn -= item.second;
if (sn) printf("No %d\n", sn);
else printf("Yes %d\n", sp);
return 0;
}
1095 Cars on Campus
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
struct Event
{
int tm, status;
bool operator< (const Event &t) const
{
return tm < t.tm;
}
};
int get(vector<Event>& ets)
{
int res = 0;
for (int i = 0; i < ets.size(); i += 2)
res += ets[i + 1].tm - ets[i].tm;
return res;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
unordered_map<string, vector<Event>> cars;
char id[10], status[10];
for (int i = 0; i < n; i ++ )
{
int hh, mm, ss;
scanf("%s %d:%d:%d %s", id, &hh, &mm, &ss, status);
int t = hh * 3600 + mm * 60 + ss;
int s = 0;
if (status[0] == 'o') s = 1;
cars[id].push_back({t, s});
}
vector<Event> events;
for (auto& item : cars)
{
auto& ets = item.second;
sort(ets.begin(), ets.end());
int k = 0;
for (int i = 0; i < ets.size(); i ++ )
if (ets[i].status == 0)
{
if (i + 1 < ets.size() && ets[i + 1].status == 1)
{
ets[k ++ ] = ets[i];
ets[k ++ ] = ets[i + 1];
i ++ ;
}
}
ets.erase(ets.begin() + k, ets.end());
for (int i = 0; i < k; i ++ ) events.push_back(ets[i]);
}
sort(events.begin(), events.end());
int k = 0, sum = 0;
while (m -- )
{
int hh, mm, ss;
scanf("%d:%d:%d", &hh, &mm, &ss);
int t = hh * 3600 + mm * 60 + ss;
while (k < events.size() && events[k].tm <= t)
{
if (events[k].status == 0) sum ++ ;
else sum -- ;
k ++ ;
}
printf("%d\n", sum);
}
int maxt = 0;
for (auto& item : cars) maxt = max(maxt, get(item.second));
vector<string> res;
for (auto& item : cars)
if (get(item.second) == maxt)
res.push_back(item.first);
sort(res.begin(), res.end());
for (int i = 0; i < res.size(); i ++ ) printf("%s ", res[i].c_str());
printf("%02d:%02d:%02d\n", maxt / 3600, maxt % 3600 / 60, maxt % 60);
return 0;
}
1105 Spiral Matrix
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 10010;
int n;
int w[N];
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> w[i];
sort(w, w + n, greater<int>());
int r, c;
for (int i = 1; i <= n / i; i ++ )
if (n % i == 0)
{
r = n / i;
c = i;
}
vector<vector<int>> res(r, vector<int>(c));
int dx[] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
for (int i = 0, x = 0, y = 0, d = 1; i < n; i ++ )
{
res[x][y] = w[i];
int a = x + dx[d], b = y + dy[d];
if (a < 0 || a >= r || b < 0 || b >= c || res[a][b])
{
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
}
for (int i = 0; i < r; i ++ )
{
cout << res[i][0];
for (int j = 1; j < c; j ++ )
cout << ' ' << res[i][j];
cout << endl;
}
return 0;
}
1109 Group Photo
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
int n, m;
struct Person
{
string name;
int h;
bool operator< (const Person &t) const
{
if (h != t.h) return h > t.h;
return name < t.name;
}
}p[N];
string line[N];
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ ) cin >> p[i].name >> p[i].h;
sort(p, p + n);
for (int i = 0, j = 0; i < m; i ++ )
{
int len = n / m;
if (!i) len += n % m; // 特判最后一排
for (int r = len / 2 + 1, l = r - 1; l > 0 || r <= len; l --, r ++ )
{
if (r <= len) line[r] = p[j ++ ].name;
if (l > 0) line[l] = p[j ++ ].name;
}
cout << line[1];
for (int k = 2; k <= len; k ++ ) cout << ' ' << line[k];
cout << endl;
}
return 0;
}
1121 Damn Single
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>
using namespace std;
const int N = 50010, M = 10010;
struct Couple
{
int a, b;
}c[N];
int ans[M];
int main()
{
int n, m;
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d%d", &c[i].a, &c[i].b);
scanf("%d", &m);
unordered_set<int> S;
for (int i = 0; i < m; i ++ )
{
int id;
scanf("%d", &id);
S.insert(id);
}
for (int i = 0; i < n; i ++ )
{
int a = c[i].a, b = c[i].b;
if (S.count(a) && S.count(b))
{
S.erase(a);
S.erase(b);
}
}
int k = 0;
for (auto id : S) ans[k ++ ] = id;
sort(ans, ans + k);
printf("%d\n", k);
if (k)
{
printf("%05d", ans[0]);
for (int i = 1; i < k; i ++ ) printf(" %05d", ans[i]);
}
return 0;
}
1128 N Queens Puzzle
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1010;
int n;
bool row[N], dg[N * 2], udg[N * 2];
int main()
{
int T;
scanf("%d", &T);
while (T -- )
{
scanf("%d", &n);
memset(row, 0, sizeof row);
memset(dg, 0, sizeof dg);
memset(udg, 0, sizeof udg);
bool success = true;
for (int y = 1; y <= n; y ++ )
{
int x;
scanf("%d", &x);
if (row[x] || dg[x + y] || udg[y - x + n]) success = false;
row[x] = dg[x + y] = udg[y - x + n] = true;
}
if (success) puts("YES");
else puts("NO");
}
return 0;
}
1129 Recommendation System
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 50010;
int n, m;
int cnt[N];
int top_k[11];
int main()
{
scanf("%d%d", &n, &m);
int k = 0;
for (int i = 0; i < n; i ++ )
{
int id;
scanf("%d", &id);
if (i)
{
printf("%d:", id);
for (int j = 0; j < k; j ++ ) printf(" %d", top_k[j]);
puts("");
}
cnt[id] ++ ;
bool exists = false;
for (int j = 0; j < k; j ++ )
if (top_k[j] == id)
{
exists = true;
break;
}
if (!exists) top_k[k ++ ] = id;
sort(top_k, top_k + k, [](int x, int y){
if (cnt[x] != cnt[y]) return cnt[x] > cnt[y];
return x < y;
});
k = min(k, m);
}
return 0;
}
1132 Cut Integer
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int T;
cin >> T;
while (T -- )
{
string number;
cin >> number;
int len = number.size() / 2;
int left = stoi(number.substr(0, len));
int right = stoi(number.substr(len));
int n = stoi(number);
if (left * right && n % (left * right) == 0) puts("Yes");
else puts("No");
}
return 0;
}
1140 Look-and-say Sequence
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int d, n;
cin >> d >> n;
string cur = to_string(d);
for (int k = 0; k < n - 1; k ++ )
{
string next;
for (int i = 0; i < cur.size();)
{
int j = i + 1;
while (j < cur.size() && cur[i] == cur[j]) j ++ ;
next += cur[i] + to_string(j - i);
i = j;
}
cur = next;
}
cout << cur << endl;
return 0;
}
1147 Heaps
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1010;
int n;
int h[N];
void dfs(int u)
{
if (u * 2 <= n) dfs(u * 2);
if (u * 2 + 1 <= n) dfs(u * 2 + 1);
printf("%d", h[u]);
if (u != 1) printf(" ");
}
int main()
{
int T;
scanf("%d%d", &T, &n);
while (T -- )
{
for (int i = 1; i <= n; i ++ ) scanf("%d", &h[i]);
bool lt = false, gt = false;
for (int i = 1; i <= n; i ++ )
for (int j = 0; j < 2; j ++ )
if (i * 2 + j <= n)
{
int a = h[i], b = h[i * 2 + j];
if (a < b) lt = true;
else gt = true;
}
if (lt && gt) puts("Not Heap");
else if (lt) puts("Min Heap");
else puts("Max Heap");
dfs(1);
puts("");
}
return 0;
}