# [the first programming competition of Guilin Institute of information technology] complete solution C++

### A.Hello Xiaoming

As the simplest check-in question, this question is adapted from hello world to investigate your string input and output ability.

#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
cout<<"Hello "<<s<<endl;
return 0;
}


### B. Knowledgeable Xiao Ming

Match the Abandon/abandon in the sentence. Because there are spaces, use getline in c + + to read the whole line. Because there is no need to consider the suffix, you can also divide the string into a single word or punctuation according to the space.
I'm going to go through the whole line of sentences directly. When you encounter a or a, you enter the judgment to judge whether the next one is b, whether the next one is a, and so on. In particular, in order to reduce the difficulty, if the prefix of the whole word is not satisfied, it is impossible for the suffix of the word to contain abandon.

#include<bits/stdc++.h>
using namespace std;
string t="abandon";
int ans;
int main(){
string s;
getline(cin,s);
for(int i=0;i<s.size();i++){
if(s[i]==t[0]||s[i]=='A'){
int idx=i;
for(int j=1;j<=6;j++){
if(s[++i]==t[j]) ;
else{
i=idx;
break;
}
}
if(i!=idx){
ans++;
}
}
}
cout<<ans<<endl;
return 0;
}


### C. Xiao mingai even number

The title is frightening, but it is very simple after clarifying the meaning of the title.
The title means to give a n n n, its sub interval starting from the starting position can be inverted so that the final number becomes an even number.
The final result set is { − 1 , 0 , 1 , 2 } \{-1,0,1,2\} {−1,0,1,2} . After reading the question, it is not difficult to find that if it can be turned into an even number, it can be realized at most twice. The first time it ends with the even number of the bit, invert it to make it the first, and then invert it as a whole to make it the last.

• When n n When each bit of n is an odd number, the result is - 1
• if when n n When the last bit of n is even, the result is 0
• else if n n When the first number of n is even, the result is 1
• else if n n When a position of n is even, the result is 2
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
if(n%2==0) cout<<0<<endl;
else{
vector<int> ve;
while(n){
int tem=n%10;
ve.push_back(tem);
n/=10;
}
int ans=-1;
for(int i=ve.size()-1,j=1;i>=0;i--,j++){
if(ve[i]%2==0){
ans=j;
break;
}
}
if(ans>=2) ans=2;
cout<<ans<<endl;
}
}
return 0;
}


### D. Xiao Ming likes typing words

Another topic is very frightening, but it is very simple after clarifying the meaning of the topic.
First, we use the map key value pair to record the position of each character in the keyboard. Similarly, with the array, we can record a~z the position of each character. Then traverse the given word, starting from the second bit, and directly accumulate the distance difference between the i and i-1 characters of the word in the keyboard. Remember to take the absolute value.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<char,int> mp;
int main(){
int t;
cin>>t;
while(t--){
mp.clear();
string s;
cin>>s;
for(int i=0;i<s.size();i++){
mp[s[i]]=i;
}
string opp;
cin>>opp;
ll ans=0;
for(int i=1;i<opp.size();i++){
ans+=abs(mp[opp[i]]-mp[opp[i-1]]);
}
cout<<ans<<endl;
}
return 0;
}


### E. Xiao Ming plays basketball

Xiao Ming is on the third floor n n If you use moves in n innings, you will win. The winning rate in other games is 50%
Moreover, according to the hint of this sentence:
Explanation: the condition for winning is that Xiao Ming is here x x Win in x Games ( x + 1 ) 2 \frac{(x+1)}{2} More than 2(x+1) innings, such as 2 wins in 3 innings, 3 wins in 5 innings
You can list formulas: ( x + 1 ) 2 = n \frac{(x+1)}{2}=n 2(x+1)​=n
Solution: x = 2 ⋅ n − 1 x=2·n-1 x=2 ⋅ n − 1 is the final answer.
Explanation: Xiao Ming will be in addition to the second n n Total of n bureaus x − 1 x-1 Win half of the even innings in x − 1 inning.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
cout<<(2*n-1)<<endl;
}
return 0;
}


### F. Xiao Ming loves mathematics

This question is also a very simple check-in question. I worked out half of the question and suddenly felt that the front was a little difficult, so I added a simple question. I'm a little selfish. I named myself.
Students who have written Fibonacci series should be able to see the result at a glance, give the first term and the second term, and then use the formula to simulate to the second term n n Item n, because n n n is relatively small, using an array.

#include<bits/stdc++.h>
using namespace std;
int f[105];
int main(){
int n,a,b;
cin>>n>>a>>b;
f[1]=a;
f[2]=b;
for(int i=3;i<=n;i++){
f[i]=(f[i-1]+f[i-2])%8;
}
cout<<f[n]<<endl;
return 0;
}


### G. Xiao Ming and Zhang San

How many squares can you go out by command.
It's a small simulation problem of search class. It was supposed to be pruned, but because c + + and Java and Python in oj can only have the same time limit, considering that there may be TLE, there is no card data, and I don't know if I can write without pruning. Anyway, I haven't started the game when I write the problem solution.
The writing method is to take each point as the starting point, search, move up, down, left and right through instructions, and then judge whether it is in the cycle (failure) or has gone out of the boundary (success).
There are two ways to judge whether to loop. One is to access the previously accessed points and record the accessed points in advance with the vis [] array; Second, is the depth of the deep search more than n ⋅ m n·m Step n ⋅ m, that means it must be a cycle.
Finally, it can be output by counting the number of points that can go out.
Pruning: when the point reaches the point that has been recorded before it can go out, there is no need to search down, and the value of true can be returned directly, indicating that it has successfully walked out of the boundary.

#include<bits/stdc++.h>
using namespace std;
char mp[1005][1005];
int vis[1005][1005];
int ris[1005][1005];
int n,m;
// W A S D
int dir[4][2]={
{-1,0},{0,-1},{1,0},{0,1}
};
map<char,int> mpp;
bool check(int x,int y){
if(x<1||x>n||y<1||y>m) return true;
else return false;
}
bool dfs(int x,int y){
if(check(x,y)){
ris[x][y]=1;
return true;
}
int k=mpp[mp[x][y]];
int xx=x+dir[k][0],yy=y+dir[k][1];
if(ris[xx][yy]) return true;
if(vis[xx][yy]) return false;
vis[xx][yy]=1;
if(dfs(xx,yy)){
ris[xx][yy]=1;
return true;
}else{
return false;
}
}
int main(){
ios::sync_with_stdio(false);
mpp['W']=0;
mpp['A']=1;
mpp['S']=2;
mpp['D']=3;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>mp[i][j];
}
}
int ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
vis[i][j]=1;
if(dfs(i,j)){
ans++;
}
vis[i][j]=0;
}
}
cout<<ans<<endl;
return 0;
}


### H. Xiaoming takes the treasure

Add a condition that you must take the first item.
Just read this item at the beginning, reduce the corresponding backpack capacity, and then pretend that this item does not exist.
01 backpack:
You can pass the question in two dimensions.
dp[i][j] represents the maximum value of the i-th item when j remains in the backpack capacity.
First dimensional circulation item, second dimensional circulation backpack capacity.
One dimension needs to flashback the second cycle, because the state in knapsack capacity < = J is still the state of i-1.

If the current object can be packed on the back, the value of the i-th object in and out of the backpack shall be calculated respectively in the traversal process, and the larger one shall be taken as the current maximum value.
If the current object cannot be packed on the back, the i-th object has the option of not putting it in the backpack.

When not in the backpack: i i Maximum value after i decision and the second decision i − 1 i-1 The value of i − 1 decision is the same (it is still the original objects, no more or less).
When putting in the backpack: i i The value after the i decision is the second i − 1 i-1 i − the value of the first decision plus the value of the current object v[j]. When the object is put into the backpack, the capacity of the backpack will become j j j. That is, the capacity of the backpack before putting objects is j - w[i].

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int dp[N];
int v[N],w[N];
int n,m;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
m-=v[1];
for(int i=2;i<=n;i++){
for(int j=m;j>=v[i];j--){
dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
}
}
cout<<dp[m]+w[1]<<endl;
return 0;
}


### 1. Xiao Ming's test results

I filled in another simple question.
Looks like it's hard again.
This question is the question of investigation and ranking. Even if it bubbles, it can pass.
Just give it to you in the form of a two-dimensional array. Just save all the given numbers, especially record the results of Xiaoming's position, and then sort them. After sorting, find the subscript of Xiaoming's position in descending order, that is, Xiaoming's score.

#include<bits/stdc++.h>
using namespace std;
vector<int> ve;
bool cmp(int x,int y){
return x>y;
}
int main(){
int n,m;
int x,y,k,w;
cin>>n>>m;
cin>>x>>y;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>k;
ve.push_back(k);
if(i==x&&j==y) w=k;
}
}
sort(ve.begin(),ve.end(),cmp);
for(int i=0;i<n*m;i++){
if(ve[i]==w){
cout<<i+1<<endl;
break;
}
}
return 0;
}


### J. Xiao Ming and flowers

The application of the inclusion exclusion principle is investigated.
Because the black box data is simple, multiple backpacks can also be done.
Because the black box data is simple, it is possible to do it indiscriminately.
The positive solution is as follows:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
int down;
ll n,m;
ll a[22];
int qpow(int a,int b){
int res=1;
while(b){
if(b&1) res=1LL*res*a%mod;
a=1LL*a*a%mod;
b>>=1;
}
return res;
}
int C(ll a,ll b){
if(a<b) return 0;
int up=1;
for(ll i=a;i>a-b;i--) up=i%mod*up%mod;
return 1LL*up*down%mod;
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>a[i];
}
down=1;
for(int i=1;i<=n-1;i++) down=1LL*i%mod*down%mod;
down=qpow(down,mod-2)%mod;
int res=0;
for(int i=0;i<(1<<n);i++){
ll d=m+n-1;
int flag=1;
for(int j=0;j<n;j++){
if((i>>j)&1){
flag*=-1;
d-=(a[j]+1);
}
}
res=(res+flag*C(d,n-1))%mod;
}
cout<<(res+mod)%mod;
return 0;
}


### K. Xiaoming and Xiaohong's addition and deletion map game

There is no detailed explanation. I predict that no one has answered this question.
Although my data is too weak, if I can understand that the answer is only 0 , 1 , 2 {0,1,2} 0,1, and 2. You can also use random numbers to pass the questions. Anyway, three groups of data are output randomly 0 , 1 , 2 {0,1,2} 0,1,2 hand it in a few more times. If you have a good face, you'll pass the question.
The serious problem-solving method is: Dijkstra judges the minimum ring if the sum of the weights of the minimum ring is less than c c c, then you can buy a ring, because only one acyclic graph can be deleted at a time, so output 2; If the minimum ring weight is greater than or equal to c c c, or the ring cannot be formed, output 1; If Xiao Ming doesn't buy anything, output 0

#include<bits/stdc++.h>
using namespace std;
const int N=2050,M=5050;
const int INF=0x3f3f3f3f;
struct node{
int to,next;
int val;
}edge[M];
int d[N][N],cnt;
int n,m,c;
int vis[M];
priority_queue<pair<int,int> > q;
void init(){
top=0;
}
edge[top].to=v;
edge[top].val=w;
}
void dijkstra(int start){
memset(vis,0,sizeof(vis));
d[start][start]=0;
q.push(make_pair(0,start));
while(!q.empty()){
auto temp=q.top();
int x=temp.second;
q.pop();
if(vis[x]) continue;
vis[x]=1;
int y=edge[i].to;
int z=edge[i].val;
if(d[start][y]>d[start][x]+z){
d[start][y]=d[start][x]+z;
q.push(make_pair(-d[start][y],y));
}
}
}
}
int main(){
memset(d,INF,sizeof(d));
init();
int n,m,c;
cin>>n>>m>>c;
for(int i=1;i<=m;i++){
int x,y,z;
cin>>x>>y>>z;
if(z<=c){
cnt++;
}
}
int minn=INF;
for(int i=1;i<=n;i++){
dijkstra(i);
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i!=j) minn=min(d[i][j]+d[j][i],minn);
}
}
if(cnt==0) cout<<0<<endl;
else{
if(minn<=c) cout<<2<<endl;
else cout<<1<<endl;
}
}


### Postscript

Except for the check-in questions and the two template questions (01 knapsack template and inclusion exclusion principle template), all the questions are the original question adaptation on codeforce and the original question adaptation of acm competition. They are also the questions I did in the competition some time ago. All the questions belong to the check-in questions in the formal competition. I hope you can slowly understand and fill out the following questions and have more interest in the algorithm.
Learning algorithms can improve your code strength.
If you work hard enough and are lucky enough, you may find a gold medal.

Record the process of compiling my data, compile it in front of others, and then compile it yourself, write the configuration file and write the question surface. Finally, if you are a big man, contact me and take me. Take me. Take me.

Tags: C++ Algorithm

Posted on Sun, 05 Dec 2021 18:41:20 -0500 by datoshway