1021-1030
1021 Deepest Root(25)
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 10001;
vector<int> adj[maxn];
bool visted[maxn] = {false};
int deep[maxn];
int max_ans = 0;
int cnt = 0;
void dfs(int u){
for(int i = 0; i < adj[u].size();i++){
if(visted[adj[u][i]]){
continue;
}else{
visted[adj[u][i]] = true;
dfs(adj[u][i]);
}
}
}
void dfs_for_deep(int u, int d, int root){
if(!visted[u]){
visted[u] = true;
for(int i = 0; i < adj[u].size(); i++){
if(!visted[adj[u][i]]){
deep[root] = max(deep[root], d + 1);
max_ans = max(max_ans, deep[root]);
dfs_for_deep(adj[u][i], d + 1, root);
}
}
}
}
int main(){
int n;
cin >> n;
if(n == 1){
cout << 1;
return 0;
}
for(int i = 0; i < n; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1; i <= n; i++){
if(visted[i]){
continue;
}
dfs(i);
cnt++;
}
if(cnt >= 2){
cout << "Error: " << cnt << " components";
return 0;
}
for(int i = 1; i <= n ;i++){
deep[i] = 1;
}
for(int i = 1; i <= n ;i++){
memset(visted, false, sizeof(visted));
dfs_for_deep(i, 1, i);
}
for(int i = 1; i <= n; i++){
if(deep[i] == max_ans){
cout << i << endl;
}
}
return 0;
}
1022 Digital Library(30)
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
map<string, vector<string> > m[6];
int main(){
int n, k;
string id, title, author, keywords, publisher, year;
cin >> n;
getchar();//吸收换行符
for(int i = 0; i < n; i++){
getline(cin, id);
getline(cin, title);
getline(cin, author);
m[1][title].push_back(id);
m[2][author].push_back(id);
// keywords要区别对待
// getline(cin, keywords);
while(cin >> keywords){
m[3][keywords].push_back(id);
char c = getchar();
if(c == '\n') break;
}
getline(cin, publisher);
getline(cin, year);
m[4][publisher].push_back(id);
m[5][year].push_back(id);
}
cin >> k;
int type;
for(int i = 0; i < k; i++){
scanf("%d: ", &type);
string temp;
getline(cin, temp);
cout << type << ": " << temp<< endl;
if(m[type][temp].size() == 0){
cout << "Not Found" << endl;
continue;
}else{
sort(m[type][temp].begin(), m[type][temp].end());
for(int j = 0; j < m[type][temp].size(); j++){
cout << m[type][temp][j] << endl;
}
}
}
return 0;
}
1023 Have Fun with Numbers(20)
dict(num) ?= dict(num * 2)
原本用两个字典存储*2前后的变化,其实没必要,因为如果相等全是0
#include<string>
#include<iostream>
using namespace std;
int ans[22] = {0};
int dict[10] = {0};
int main(){
string input;
cin >> input;
int ans_len = 1;
for(int i = input.length() - 1; i >= 0; i--){
//set dict1
int temp = input[i] - '0';
dict[temp]++;
temp *= 2;
ans[ans_len] += temp;
//进位
int t = ans[ans_len];
ans[ans_len] = t % 10;
ans[ans_len + 1] += t / 10;
ans_len++;
dict[t % 10]--;
}
bool flag = true;
for(int i = 1; i <= 9; i++){
if(dict[i] != 0){
flag = false;
}
}
flag == true ? printf("Yes\n") : printf("No\n");
for(int i = ans_len; i >= 1; i--){
if(i == ans_len && ans[i] == 0){
continue;
}
printf("%d",ans[i]);
}
}
1024 Palindromic Number(25)
isPalindromic((n + reverse(n))
可以用string自带的reverse函数来实现翻转
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int num[100], len = 0;
int temp[100];
bool judge(){
for(int i = 1; i <= len / 2; i++){
if(num[i] != num[len - i + 1]){
return false;
}
}
return true;
}
int main(){
string n;
int k;
cin >> n >> k;
int step = 0;
for(int i = n.length() - 1; i >= 0; i--){
num[++len] = n[i] - '0';
}
if(judge()){
for(int i = len; i >= 1; i--){
cout << num[i];
}
cout << endl << step;
return 0;
}
while(step < k){
step++;
//reverse
for(int i = 1; i <= len; i++){
temp[i] = num[len - i + 1];
}
//add
int carry = 0;
for(int i = 1; i <= len; i++){
num[i] = temp[i] + num[i] + carry;
carry = 0;
if(num[i] > 9){
num[i] -= 10;
carry = 1;
}
}
if(carry == 1){
num[++len] = carry;
}
if(judge()){
break;
}
}
for(int i = len; i >= 1; i--){
cout << num[i];
}
cout << endl << step;
return 0;
}
1025 PAT Ranking(25)
排序时注意多个条件,如分数相同时序号小的在前;rank会存在相同的。
#include<iostream>
#include<algorithm>
using namespace std;
struct User{
string id;
int score;
int final_rank, local_rank;
int location_number;
}user[30001];
bool cmp(User u1, User u2){
//return u1.score > u2.score;
return u1.score == u2.score ? u1.id < u2.id : u1.score > u2.score;
}
int main(){
int n, k;
int user_num = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", &k);
for(int j = 1; j <= k; j++){
cin >> user[user_num + j].id >> user[user_num + j].score;
user[user_num + j].location_number = i;
}
sort(user + user_num + 1, user + user_num + k + 1,cmp);
user[user_num + 1].local_rank = 1;
for(int j = 2; j <= k; j++){
if(user[user_num + j].score == user[user_num + j - 1].score){
user[user_num + j].local_rank = user[user_num + j - 1].local_rank;
}else{
user[user_num + j].local_rank = j;
}
}
user_num += k;
}
sort(user + 1, user + user_num + 1, cmp);
user[1].final_rank = 1;
for(int i = 2; i <= user_num; i++){
if(user[i].score == user[i - 1].score){
user[i].final_rank = user[i - 1].final_rank;
}else{
user[i].final_rank = i;
}
}
cout << user_num << endl;
for(int i = 1; i <= user_num; i++){
cout << user[i].id << " " << user[i].final_rank << " "
<< user[i].location_number << " " << user[i].local_rank
<< endl;
}
}
1026 Table Tennis(30)
#include<iostream>
#include<queue>
#include<map>
using namespace std;
const int maxT = 100000;//最大时间 以秒为单位
const int maxn = 10001;//最大测试样例数
int table, vtable;//记录空闲的普通桌子和vip桌子
int now, nowt;//记录当前时间和当前时间使用的桌子编号
//T和V分别存储t时刻到达用户的play time和vip状态
int T[maxT], V[maxT];
int vip[maxn];//存储桌子的vip状态
int AnsI[maxn], AnsO[maxn], num[maxn];//存储最后结果
map<int, int> Table;//记录Table i对应的剩余使用时间
queue<int> Wait, vWait;//分别存储普通用户和vip用户的到达时间
int main(){
int cnt = 0;
int n, m, k;
cin >> n;
for(int i = 0; i < n; i++){
int H, M, S;
scanf("%d:%d:%d", &H, &M, &S);
int t = H * 3600 + M * 60 + S;
scanf("%d %d", &T[t], &V[t]);
T[t] = min(T[t], 120) * 60;
}
cin >> m >> k;
for(int i = 0; i < k; i++){
int temp;
cin >> temp;
vip[temp] = 1;
}
//模拟每一秒
for(int Time = 28800; Time < 75600; Time++){
now = 0, table = 0, vtable = 0;
//Time时刻有vip用户进入
if(T[Time] && V[Time]){
vWait.push(Time);
}else if(T[Time]){
//普通用户
Wait.push(Time);
}
for(int i = 1; i <= m; i++){
if(Table[i] > 0) Table[i]--;
if(Table[i] == 0 && vip[i] && vtable == 0){
vtable = i;
}
if(Table[i] == 0 && table ==0){
table = i;
}
}
if(!vWait.empty() && (table || vtable)){
now = vWait.front();
if(vtable != 0){
nowt = vtable;
vWait.pop();
}else{
nowt = table;
if(!Wait.empty() && Wait.front() < vWait.front()){
now = Wait.front();
Wait.pop();
}else{
vWait.pop();
}
}
}else if(!Wait.empty() && (table || vtable)){
if(table != 0){
nowt = table;
}else{
nowt = vtable;
}
now = Wait.front();
Wait.pop();
}
if(now == 0){
continue;
}
Table[nowt] = T[now];
AnsI[cnt] = now;
AnsO[cnt] = Time;
cnt++;
num[nowt]++;
}
for(int i = 0; i < cnt; i++){
//这里加30s后转化为分钟级
printf("%02d:%02d:%02d %02d:%02d:%02d %d\n",
AnsI[i] / 3600, AnsI[i] % 3600 / 60, AnsI[i] % 60,
AnsO[i] / 3600, AnsO[i] % 3600 / 60, AnsO[i] % 60,
(AnsO[i] - AnsI[i] + 30) / 60);
}
for(int i = 1; i <= m; i++){
if(i != 1) cout << " ";
cout << num[i];
}
return 0;
}
1027 Colors in Mars(20)
#include<cstdio>
using namespace std;
char ans[14] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'};
int main(){
printf("#");
int n;
for(int i = 1; i <= 3; i++){
scanf("%d", &n);
printf("%c%c", ans[n / 13], ans[n % 13]);
}
}
1028 List Sorting(25)
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct student{
int id;
string name;
int score;
}stu[100001];
int n, c;
bool cmp(student s1, student s2){
if(c == 1){
return s1.id < s2.id;
}else if(c == 2){
if(s1.name == s2.name){
return s1.id < s2.id;
}
return s1.name < s2.name;
}else{
if(s1.score == s2.score){
return s1.id < s2.id;
}
return s1.score < s2.score;
}
}
int main(){
scanf("%d%d", &n, &c);
for(int i = 0; i < n; i++){
cin >> stu[i].id >> stu[i].name >> stu[i].score;
}
sort(stu, stu + n, cmp);
for(int i = 0; i < n; i++){
printf("%06d ",stu[i].id);
cout << stu[i].name << " " << stu[i].score << endl;
}
}
1029 Median(25)
#include<iostream>
#include<set>
using namespace std;
set<int> s;
int main(){
int n, k;
for(int i = 0; i < 2; i++){
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d", &k);
s.insert(k);
}
}
set<int>::iterator i;
int cnt = (s.size() - 1) / 2;
for(i = s.begin(); i != s.end(); i++){
if(cnt == 0){
cout << *i;
}
cnt--;
}
}
1030 Travel Plan(30)
#include<iostream>
#include<vector>
using namespace std;
const int INF = 1e9;
const int maxn = 501;
int adj[maxn][maxn], cost[maxn][maxn], dis[maxn];
bool visited[maxn] = {false};
vector<int> path, temppath;
vector<int> pre[maxn];
int mincost = INF;
int n, m, s, d;
void dfs(int v){
temppath.push_back(v);
if(v == s){
int tempcost = 0;
for(int i = temppath.size() - 1; i > 0; i--){
tempcost += cost[temppath[i]][temppath[i - 1]];
}
if(tempcost < mincost){
mincost = tempcost;
path = temppath;
}
temppath.pop_back();
return;
}
for(int i = 0; i < pre[v].size(); i++){
dfs(pre[v][i]);
}
temppath.pop_back();
}
int main(){
fill(adj[0], adj[0] + maxn * maxn, INF);
fill(dis, dis + maxn, INF);
cin >> n >> m >> s >> d;
for(int i = 0; i < m; i++){
int c1, c2, distance, c;
cin >> c1 >> c2 >> distance >> c;
adj[c1][c2] = adj[c2][c1]= distance;
cost[c1][c2] = cost[c2][c1] = c;
}
//这里注意是s 开始写0能通过测试样例但是通不过其它
dis[s] = 0;
for(int i = 0; i < n; i++){
int u = -1, minn = INF;
for(int v = 0; v < n; v++){
if(!visited[v] && dis[v] < minn){
minn = dis[v];
u = v;
}
}
if(u == -1){
break;
}
visited[u] = true;
for(int v = 0; v < n; v++){
// if(!visited[v] && dis[u] + adj[u][v] < dis[v]){
// dis[v] = dis[u] + adj[u][v];
// pre[v].clear();
// pre[v].push_back(u);
// }else if(!visited[v] && dis[v] == dis[u] + adj[u][v]){
// pre[v].push_back(u);
// }
if(!visited[v] && adj[u][v] != INF){
if(dis[v] > dis[u] + adj[u][v]){
dis[v] = dis[u] + adj[u][v];
pre[v].clear();
pre[v].push_back(u);
}else if(dis[v] == dis[u] + adj[u][v]){
pre[v].push_back(u);
}
}
}
}
dfs(d);
for(int i = path.size() - 1; i >= 0; i--){
printf("%d ", path[i]);
}
printf("%d %d", dis[d], mincost);
return 0;
}