Skip to content

Commit

Permalink
🎉 feat: initial commit for pat advanced 1172 to 1175 using C++
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuo committed Nov 22, 2023
1 parent fb6cde6 commit 7d8d085
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
24 changes: 24 additions & 0 deletions AdvancedLevel_C++/1172. Panda and PP Milk (20).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
using namespace std;
int n, ans, A[10005], L[10005], R[10005];
int main(){
cin >> n;
for (int i = 1; i <= n; i++) cin >> A[i];
for (int i = 1; i < n; i++) {
int j = i;
while(j + 1 <= n && A[j] >= A[j + 1]) {
if (A[j] > A[j + 1]) ++L[i];
++j;
}
}
for (int i = n; i > 1; i--) {
int j = i;
while(j - 1 >= 1 && A[j] >= A[j - 1]) {
if (A[j] > A[j - 1]) ++R[i];
--j;
}
}
for (int i = 1; i <= n; i++) ans += max(L[i], R[i]) * 100 + 200;
cout << ans;
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
using namespace std;
int N, M, ans, a, pre[10005];
int main(){
cin >> N >> M;
for (int i = 1; i <= N; i++) {
cin >> a;
pre[i] = pre[i - 1] + a;
}
for (int i = 1; i <= N; i++) {
for (int j = i; j <= N; j++) {
if (pre[j] - pre[i - 1] <= M) ++ans;
else break;
}
}
cout << ans;
return 0;
}
24 changes: 24 additions & 0 deletions AdvancedLevel_C++/1174. Left-View of Binary Tree (25).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
using namespace std;
int N, pre[30], in[30], t[30];
void deal(int ins, int ine, int prerootindex, int level) {
if (ins > ine) return;
if (t[level] == 0) {
t[level] = pre[prerootindex];
}
int pos = ins;
while(in[pos] != pre[prerootindex]) ++pos;
deal(ins, pos - 1, prerootindex + 1, level + 1);
deal(pos + 1, ine, prerootindex + 1 + pos - ins, level + 1);
}
int main(){
cin >> N;
for (int i = 1; i <= N; i++) cin >> in[i];
for (int i = 1; i <= N; i++) cin >> pre[i];
deal(1, N, 1, 1);
for (int i = 1; t[i]; i++) {
if (i != 1) cout << ' ';
cout << t[i];
}
return 0;
}
94 changes: 94 additions & 0 deletions AdvancedLevel_C++/1175. Professional Ability Test (30).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct node {
int v, score, voucher;
bool operator < (const node &x) const {
if(score != x.score) return score > x.score;
else return voucher < x.voucher;
}
};
struct bian {
int next, S, D;
};
vector<bian> E[1005];
vector<pair<int,int>> Dis(1005, {2e9, -1});
int N, M, T1, T2, S, D, f, T, in[1005], in2[1005], Last[1005];
queue<int> DAG;
int huan() {
vector<int> S;
while(DAG.size()) {
int now = DAG.front();
DAG.pop();
S.push_back(now);
for(auto it : E[now]) {
in2[it.next]--;
if(!in2[it.next]) DAG.push(it.next);
}
}
return S.size() == N;
}
void dijkstra() {
vector<int> vis(1005);
priority_queue<node> Q;
Q.push({1002, 0, 0});
Dis[1002].first = Dis[1002].second = 0;
while(Q.size()) {
node now = Q.top();
Q.pop();
if(vis[now.v]) continue;
vis[now.v] = 1;
Dis[now.v].first = now.score;
Dis[now.v].second = now.voucher;
for (auto it : E[now.v]) {
if(vis[it.next]) continue;
if((Dis[it.next].first > Dis[now.v].first + it.S) || ((Dis[it.next].first == Dis[now.v].first + it.S) && (Dis[it.next].second < Dis[now.v].second + it.D))) {
Dis[it.next].first = Dis[now.v].first + it.S;
Dis[it.next].second = Dis[now.v].second + it.D;
Last[it.next] = now.v;
Q.push({it.next, Dis[it.next].first, Dis[it.next].second});
}
}
}
return;
}

int main() {
cin >> N >> M;
for (int i = 0; i < M; i++) {
cin >> T1 >> T2 >> S >> D;
E[T1].push_back({T2, S, D});
in[T2]++, in2[T2]++;
}
for (int i = 0; i < N; i++) {
if (in[i] == 0) {
E[1002].push_back({i, 0, 0});
DAG.push(i);
}
}
f = huan();
dijkstra();
cin >> T;
if(f) cout << "Okay.\n";
else cout << "Impossible.\n";
for (int i = 1, q; i <= T; i++) {
cin >> q;
if(!in[q]) cout << "You may take test " << q << " directly.\n";
else if(!f) cout << "Error.\n";
else {
vector<int> path;
int now = q;
while(q != 1002) {
path.push_back(q);
q = Last[q];
}
for (int j = path.size() - 1; j >= 0; j--) {
cout << path[j];
if(j) cout << "->";
}
cout << '\n';
}
}
return 0;
}

0 comments on commit 7d8d085

Please sign in to comment.