T1(L1 - 1)
cpp
#include <iostream>
using namespace std;
int main(){
cout << "Building the Future, One Line of Code at a Time.";
}
T2(L1 - 2)
cpp
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
cout << n * 15;
return 0;
}
T3(L1 - 3)
cpp
#include <iostream>
using namespace std;
int main(){
int a , b;
cin >> a >> b;
cout << b - a << '\n';
if(b - a > 250)cout << "jiu ting tu ran de...";
else if(b - a <= 0)cout << "hai sheng ma?";
else cout << "nin tai cong ming le!";
return 0;
}
T4(L1 - 4)
cpp
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int a[N],cnt;
int main(){
int n ;
cin >> n;
for(int i = 1 ; i<=n ; i++){
cin >> a[i];
if(a[i] < 1700)cnt++;
}
cout << cnt;
return 0;
}
T5(L1-5)(单纯写复杂了...)
cpp
#include <iostream>
#include <vector>
#define endx(x) {cout << (x);return 0;}
using namespace std;
const int N = 1e6 + 10;
int a[N],cnt[N];
vector<int>v;
vector<int>res;
int main(){
int n;
cin >> n;
for(int i = 1 , x , y;i <= n ; i++){
cin >> x >> y;
cnt[x] ++;
v.push_back(x);
a[x] += (y == 0);
}
sort(v.begin() , v.end());
for(auto & x : v){
if(a[x] == cnt[x]){
res.push_back(x);
a[x] = 1;
}
}
if(res.empty()){
endx("NONE");
}
for(int i = 0; i < res.size() ; i ++){
if(i)cout << ' ';
cout << res[i];
}
return 0;
}
T6(L1-6)
cpp
#include <iostream>
#include <vector>
#include <cstring>
#define endx(x) {cout << (x);return 0;}
using namespace std;
const int N = 1e6 + 10;
char ch[N];
int main(){
for(int i = 1 ; i<= 11 ; i++){
string s;
getline(cin , s);
cout << s.size() ;
}
return 0;
}
T7(L1-7)
cpp
#include <iostream>
#include <vector>
#include <cstring>
#define endx(x) {cout << (x);return 0;}
using namespace std;
const int N = 1e6 + 10;
vector<int>a(N);
vector<int>ans;
int main(){
int n;
cin >> n;
for(int i = 1 ; i <= n ; i ++){
cin >> a[i];
}
int ma = 0 , mi = 0x3f3f3f3f , sum = 0;;
for(int i =1 ; i<= n ; i++){
if(a[i] > ma)ma = a[i];
if(a[i] < mi)mi = a[i];
sum += a[i];
}
sum /= n;
cout << ma << ' ' << mi << ' ' << sum << '\n';
for(int i =1 ; i<= n ; i++){
if(a[i] > (sum << 1)){
ans.push_back(i);
}
}
if(ans.empty()){
endx("Normal")
}
for(int i = 0 ; i < ans.size() ; i ++){
if(i)cout << ' ';
cout << ans[i];
}
return 0;
}
T8(L1-8 智慧文本编辑器)
评价:标准的字符串函数题目,也没大坑,甚至可以不用子串substr()函数,出题组变良心了
cpp
#include <iostream>
#include <vector>
#include <cstring>
#include <map>
#include <stack>
#include <algorithm>
#define endx(x) {cout << (x) << '\n';return ;}
using namespace std;
const int N = 1e6 + 10;
string s;
void solve(){
int op , p , l , r;
string t;
cin >> op;
switch(op){
case 1:{
cin >> t;
vector<int>res;
int si = s.find(t);
if(si == string::npos)endx(-1)
while(si != string::npos){
//cout << si << ' ';
res.push_back(si);
si = s.find(t , si + 1);
}
for(int i = 0 ; i < min(3 , (int)res.size()) ; i ++){
if(i )cout << ' ';
cout << res[i];
}
cout << '\n';
break;
}
case 2:
cin >> p >> t;
s.insert(p ,t);
cout << s << '\n';
break;
case 3:
cin >> l >> r;
reverse(s.begin() + l , s.begin() + r + 1);
cout << s <<'\n';
}
}
int main(){
int n;
cin >> n >> s;
while(n --)solve();
return 0;
}
T9 (L2-1 姥姥改作业)
标准的栈的中型模拟题目,思路:
设置两个栈,一个是当前这一堆作业一个是左边一堆作业。
一开始先读入并且模拟放入左边一堆作业的栈中
然后就是两个栈互相交换去模拟。
cpp
#include <iostream>
#include <vector>
#include <cstring>
#include <map>
#include <stack>
#define endx(x) {cout << (x);return 0;}
using namespace std;
const int N = 1e6 + 10;
int a[N];
vector<int>res;
stack<int>st, le;
int main(){
int n , m = 0 , t , sum = 0;
cin >> n >> t;
for(int i =1 ; i<= n ; i++){
cin >> a[i];
if(a[i] <= t && ++m)res.push_back(i);
else {st.push(i);sum += a[i];}
}
int x , k = 1;
while(m < n){
if(k & 1){
t = sum / st.size();sum = 0;
while(!st.empty()){
x = st.top();st.pop();
if(a[x] <= t && ++m)res.push_back(x);
else {le.push(x);sum += a[x];}
}
}else{
t = sum / le.size();sum = 0;
while(!le.empty()){
x = le.top();le.pop();
if(a[x] <= t && ++m)res.push_back(x);
else {st.push(x);sum += a[x];}
}
}
k ^= 1;
}
for(int i = 0; i < n ; i ++){
if(i)cout << ' ';
cout << res[i];
}
return 0;
}
T10 (L2-2 超参数搜索)
一开始还以为考单调队列,写了第一版后发现排个序可以直接找,大于等于 xxx中最小的,那排序后要寻找(某个值/大于等于某个值的最小值)的优化自然就是可以二分。
cpp
#include <iostream>
#include <vector>
#include <cstring>
#include <map>
#include <stack>
#define endx(x) {cout << (x);return 0;}
using namespace std;
const int N = 1e6 + 10;
int a[N] , bi;
pair<int,int> b[N];
vector<int>res;
int main(){
int n;
cin >> n;
for(int i = 1; i <= n ; i ++){
cin >> a[i];
b[i] = {a[i] , i};
}
sort(b + 1 , b + n + 1);
int mx = 0;
for(int i = 1 ; i<= n ; i++){
if(a[i] > mx){
mx = a[i];
res.clear();
res.push_back(i);
}else if(a[i] == mx) res.push_back(i);
}
for(int i = 0; i < res.size() ; i ++){
if(i)cout << ' ';
cout << res[i];
}
cout << '\n';
int q , l , r , mid , x;
cin >> q;
while(q --){
cin >> x;
l = 1 , r = n;
while(l < r){
mid = (l + r) >> 1;
if(b[mid].first<=x){
l = mid + 1;
}else r = mid;
}
if(b[r].first > x){
while(b[r - 1].first > x)r --;//处理编号最小的解
cout << b[r].second << '\n';
}else cout << 0 << '\n';
}
return 0;
}
T11(L2-3 森林藏宝图)
看出其实是一颗树就好写很多了,从叶子往根走还能暴力个21分,超时原因:如果树是从根往下一直是一条链,并且在最后一个节点又接了很多叶子结点就会重复很多不必要的操作
正解可以是DFS,也可以是BFS;
其中mp[x]记录了从根节点到x节点路径上边权(小路安全系数)最小值
AC代码
cpp
#include <iostream>
#include <vector>
#include <cstring>
#include <map>
#define endx(x) {cout << (x);return 0;}
using namespace std;
const int N = 1e6 + 10;
pair<int,int> fa[N];
int cnt[N];
vector<int>res;
map<int,int>mp;
vector<pair<int,int>>v[N];
void dfs(int x,int mi){
mp[x] = mi;
for(auto &[u , w]:v[x]){
dfs(u , min(mi , w));
}
}
int main(){
int n;
cin >> n;
for(int i = 1 , x , y ; i<= n ; i ++){
cin >> x >> y;
cnt[x] ++;
fa[i] = {x , y};
v[x].push_back({i , y});
}
dfs(0,0x3f3f3f3f);
int mx = 0;
for(int i = 1 , j , mi ; i < n ; i++){
if(!cnt[i]){
mi = mp[i];
if(mi > mx){
mx = mi;
res.clear();
res.push_back(i);
}else if(mi == mx){
res.push_back(i);
}
}
}
cout << mx << '\n';
for(int i = 0 ; i < res.size() ; i++){
if(i)cout << ' ';
cout << res[i];
}
return 0;
}
21分代码
思路像并查集一样,往上一直find直到根节点
cpp
#include <iostream>
#include <vector>
#include <cstring>
#define endx(x) {cout << (x);return 0;}
using namespace std;
const int N = 1e6 + 10;
pair<int,int> fa[N];
int cnt[N];
vector<int>res;
int main(){
int n;
cin >> n;
for(int i = 1 , x , y ; i<= n ; i ++){
cin >> x >> y;
cnt[x] ++;
fa[i] = {x , y};
}
int mx = 0;
for(int i = 1 , j , mi ; i < n ; i++){
if(!cnt[i]){
j = i , mi = 0x3f3f3f3f;
while(j){
mi = min(mi , fa[j].second);
j = fa[j].first;
}
if(mi > mx){
mx = mi;
res.clear();
res.push_back(i);
}else if(mi == mx){
res.push_back(i);
}
}
}
cout << mx << '\n';
for(int i = 0 ; i < res.size() ; i++){
if(i)cout << ' ';
cout << res[i];
}
return 0;
}
T12(L2-4 大语言模型的推理)
题目中说一旦某个推理路径走到尽头 就很类似DFS的思路了
模型选择推理概率最高的子想法尝试展开,那么我们就要排序
然后记录哪个节点走过,然后就是经典的DFS记录路径
cpp
#include <iostream>
#include <vector>
#include <cstring>
#include <map>
#define endx(x) {cout << (x);return 0;}
using namespace std;
const int N = 1e6 + 10;
using pii = pair<int,int>;
vector<pair<int,int>>res[N];
vector<int>a;
bool vis[N];
int n , m;
void dfs(int u){
a.push_back(u);
vis[u] = true;
for(auto & [p , v] : res[u]){
if(vis[v])continue;
dfs(v);
return ;
}
}
int main(){
cin >> n >> m;
for(int i = 1 , x , y , p ; i <= m ; i++){
cin >> x >> y >> p;
res[x].push_back({p , y});
}
for(int i = 1 ; i <= n ; i ++){
sort(res[i].begin() , res[i].end() , [&](pii x , pii y){
if(x.first == y.first)return x.second < y.second;
return x.first > y.first;
});
}
int k , x;
cin >> k;
while(k --){
cin >> x;
a.clear();
memset(vis , false , sizeof vis);
dfs(x);
for(int i = 0 ; i < a.size() ; i++){
if(i)cout << "->" << a[i];
else cout << a[i];
}
cout << '\n';
}
return 0;
}