其他分享
首页 > 其他分享> > CF入门题刷题日记

CF入门题刷题日记

作者:互联网

基本感觉训练 英语阅读训练

目前每日计划:(洛谷难度评分)5入门(红),3普及-(橙),

20220730

红1:ICPC Balloons

题目描述

In an ICPC contest, balloons are distributed as follows:

  • Whenever a team solves a problem, that team gets a balloon.
  • The first team to solve a problem gets an additional balloon.

A contest has 26 problems, labelled $ \textsf{A} $ , $ \textsf{B} $ , $ \textsf{C} $ , ..., $ \textsf{Z} $ . You are given the order of solved problems in the contest, denoted as a string $ s $ , where the $ i $ -th character indicates that the problem $ s_i $ has been solved by some team. No team will solve the same problem twice.Determine the total number of balloons that the teams recieved. Note that some problems may be solved by none of the teams.

试做

#include <bits/stdc++.h>
using namespace std;
#define shanzr main
int shanzr(){
    int t;cin>>t;
    int n;string str;
    while(t--){
        int ans=0;
        cin>>n;cin>>str;
        int vis[30];
        memset(vis,0,sizeof vis);
        for(int i=0;i<n;i++){
            if(vis[str[i]-'A']){
                ans+=1;
            }
            else{
                vis[str[i]-'A']=1;
                ans+=2;
            }
        }
        cout<<ans<<endl;
    }
}

红2:YES or YES?

题目描述

There is a string $ s $ of length $ 3 $ , consisting of uppercase and lowercase English letters. Check if it is equal to "YES" (without quotes), where each letter can be in any case. For example, "yES", "Yes", "yes" are all allowable.

试做

#include "iostream"
using namespace std;
#define shanzr main
bool J(string str){
    if(str[0]!='Y'&&str[0]!='y')return false;
    if(str[1]!='E'&&str[1]!='e')return false;
    if(str[2]!='S'&&str[2]!='s')return false;
    return true;
}
int shanzr(){
    int t;cin>>t;
    while(t--){
        string str;cin>>str;
        if(J(str))cout<<"YES\n";
        else cout<<"NO\n";
    }
}

红3:Round Down the Price

题目描述

At the store, the salespeople want to make all prices round.

In this problem, a number that is a power of $ 10 $ is called a round number. For example, the numbers $ 10^0 = 1 $ , $ 10^1 = 10 $ , $ 10^2 = 100 $ are round numbers, but $ 20 $ , $ 110 $ and $ 256 $ are not round numbers.

So, if an item is worth $ m $ bourles (the value of the item is not greater than $ 10^9 $ ), the sellers want to change its value to the nearest round number that is not greater than $ m $ . They ask you: by how many bourles should you decrease the value of the item to make it worth exactly $ 10^k $ bourles, where the value of $ k $ — is the maximum possible ( $ k $ — any non-negative integer).

For example, let the item have a value of $ 178 $ -bourles. Then the new price of the item will be $ 100 $ , and the answer will be $ 178-100=78 $ .

试做

#include “iostream"
using namespace std;
#define shanzr main
#define ll long long  
ll t,m;
ll j(ll x){
	ll len=1;  
	while(x){
		x/=10;
		len*=10;
	}
	len/=10;
	return len;
}
int shanzr(){
	cin>>t;
	for(int i=1;i<=t;i++){
		cin>>m;
		cout<<m-j(m)<<endl; 
	}
	return 0;
} 

红4:Grass Field

题目描述

There is a field of size $ 2 \times 2 $ . Each cell of this field can either contain grass or be empty. The value $ a_{i, j} $ is $ 1 $ if the cell $ (i, j) $ contains grass, or $ 0 $ otherwise.

In one move, you can choose one row and one column and cut all the grass in this row and this column. In other words, you choose the row $ x $ and the column $ y $ , then you cut the grass in all cells $ a_{x, i} $ and all cells $ a_{i, y} $ for all $ i $ from $ 1 $ to $ 2 $ . After you cut the grass from a cell, it becomes empty (i. e. its value is replaced by $ 0 $ ).

Your task is to find the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all $ a_{i, j} $ zeros).

You have to answer $ t $ independent test cases.

试做

#include "iostream"
using namespace std;
#define shanzr main
int shanzr(){
    int t;cin>>t;
    int x;
    while(t--){
        int sum=0;
        for(int i=1;i<=4;i++){
            cin>>x;
            sum+=x;
        }
        if(sum==0)cout<<0<<endl;
        else if(sum==4)cout<<2<<endl;
        else cout<<1<<endl;
    }
}

红5:The Third Three Number Problem

题目描述

You are given a positive integer $ n $ . Your task is to find any three integers $ a $ , $ b $ and $ c $ ( $ 0 \le a, b, c \le 10^9 $ ) for which $ (a\oplus b)+(b\oplus c)+(a\oplus c)=n $ , or determine that there are no such integers.

Here $ a \oplus b $ denotes the bitwise XOR of $ a $ and $ b $ . For example, $ 2 \oplus 4 = 6 $ and $ 3 \oplus 1=2 $ .

复做

我抄这题五分钟我无思路。数论我是完全不会啊

看了题解。。。原来可以尝试分析,找出所有解中的最简单一种即可

#include "iostream"
using namespace std;
#define shanzr main
int shanzr(){
    int t;cin>>t;
    int x;
    while(t--){
        cin>>x;
        if(x&1)cout<<-1<<endl;
        else cout<<0<<' '<<x/2<<' '<<x/2<<endl;
    }
}

橙1:Making Towers

题目描述

You have a sequence of $ n $ colored blocks. The color of the $ i $ -th block is $ c_i $ , an integer between $ 1 $ and $ n $ .

You will place the blocks down in sequence on an infinite coordinate grid in the following way.

  1. Initially, you place block $ 1 $ at $ (0, 0) $ .
  2. For $ 2 \le i \le n $ , if the $ (i - 1) $ -th block is placed at position $ (x, y) $ , then the $ i $ -th block can be placed at one of positions $ (x + 1, y) $ , $ (x - 1, y) $ , $ (x, y + 1) $ (but not at position $ (x, y - 1) $ ), as long no previous block was placed at that position.

A tower is formed by $ s $ blocks such that they are placed at positions $ (x, y), (x, y + 1), \ldots, (x, y + s - 1) $ for some position $ (x, y) $ and integer $ s $ . The size of the tower is $ s $ , the number of blocks in it. A tower of color $ r $ is a tower such that all blocks in it have the color $ r $ .

For each color $ r $ from $ 1 $ to $ n $ , solve the following problem independently:

  • Find the maximum size of a tower of color $ r $ that you can form by placing down the blocks according to the rules.

infinite coordinate grid:无限坐标网格

试做

#include "iostream"
#include "cstring"
using namespace std;
const int N=100010;
#define shanzr main
int vis[N];
int ans[N];
int shanzr(){
    int t;cin>>t;
    while(t--){
        int n;cin>>n;
        int x;
        memset(vis,0,sizeof vis);
        memset(ans,0,sizeof ans);
        for(int i=1;i<=n;i++){
            cin>>x;
            if(vis[x]==0)ans[x]++;
            else if(((i-vis[x]-1)&1)==0)ans[x]++;
            vis[x]=i;
        }
        for(int i=1;i<=n;i++)cout<<ans[i]<<' ';
        cout<<endl;
    }
}

小记

读题碰到了理解问题。注意“各颜色独立求答案”。

橙2:Another String Minimization Problem

题目描述

You have a sequence $ a_1, a_2, \ldots, a_n $ of length $ n $ , consisting of integers between $ 1 $ and $ m $ . You also have a string $ s $ , consisting of $ m $ characters B.

You are going to perform the following $ n $ operations.

  • At the $ i $ -th ( $ 1 \le i \le n $ ) operation, you replace either the $ a_i $ -th or the $ (m + 1 - a_i) $ -th character of $ s $ with A. You can replace the character at any position multiple times through the operations.

Find the lexicographically smallest string you can get after these operations.

A string $ x $ is lexicographically smaller than a string $ y $ of the same length if and only if in the first position where $ x $ and $ y $ differ, the string $ x $ has a letter that appears earlier in the alphabet than the corresponding letter in $ y $ .

lexicographically:字典序

试做

#include "iostream"
#include "cstring"
using namespace std;
#define shanzr main
const int N=100010;
char str[N];
int shanzr(){
    int t;cin>>t;
    int len,ops;
    while(t--){
        cin>>ops>>len;
        int x;
        for(int i=1;i<=len;i++)str[i]='B';
        while(ops--){
            cin>>x;
            if(x>len-x+1)x=len-x+1;
            if(str[x]=='A'){
                if(str[len-x+1]=='A')continue;
                else str[len-x+1]='A';
            }
            else str[x]='A';
        }
        for(int i=1;i<=len;i++)cout<<str[i];
        cout<<endl;
    }
}

橙3:Mark the Dust Sweeper

题目描述

Mark is cleaning a row of $ n $ rooms. The $ i $ -th room has a nonnegative dust level $ a_i $ . He has a magical cleaning machine that can do the following three-step operation.

  • Select two indices $ i<j $ such that the dust levels $ a_i $ , $ a_{i+1} $ , $ \dots $ , $ a_{j-1} $ are all strictly greater than $ 0 $ .
  • Set $ a_i $ to $ a_i-1 $ .
  • Set $ a_j $ to $ a_j+1 $ .

Mark's goal is to make $ a_1 = a_2 = \ldots = a_{n-1} = 0 $ so that he can nicely sweep the $ n $ -th room. Determine the minimum number of operations needed to reach his goal.

indices:指数,index的复数

试做

#include "iostream"
#include "cstring"
using namespace std;
#define shanzr main
const int N=100010;
#define ll long long
ll num[N];
int shanzr(){
    ll t;cin>>t;
    ll len;
    while(t--){
        cin>>len;
        ll cnt=0;ll flag=true;
        for(ll i=1;i<=len;i++){
            cin>>num[i];
            if(i==len)break;
            if(num[i])flag= false;
            if(flag)continue;
            if(!num[i]){
                for(ll j=1;j<i;j++){
                    if(num[j]) {
                        num[j]--;
                        num[i]=1;
                        break;
                    }
                }
                cnt++;
            }
        }
        for(ll i=1;i<len;i++){
            cnt+=num[i];
        }
        cout<<cnt<<endl;
    }
}

算法过于垃圾以至于第三个数据TLE了。不该模拟的不要模拟。

他山之石

援引自https://www.luogu.com.cn/blog/345900/solution-cf1705b

#include<bits/stdc++.h>
using namespace std;
long long t,n,a,sum,zero;
int main(){
	cin>>t;
	while(t--){
		sum=0,zero=0;
		bool fl=false;
		cin>>n;
		for(int i=1;i<n;i++){
			cin>>a,sum+=a;
			if(fl&&!a) zero++;	//需要填充的0
			if(a!=0) fl=true;	//是否读到了有数值的数
		}
		cin>>a;
		cout<<sum+zero<<endl;
	}
	return 0;
}

20220731

红1:XOR Mixup

题目描述

There is an array $ a $ with $ n-1 $ integers. Let $ x $ be the bitwise XOR of all elements of the array. The number $ x $ is added to the end of the array $ a $ (now it has length $ n $ ), and then the elements are shuffled.

You are given the newly formed array $ a $ . What is $ x $ ? If there are multiple possible values of $ x $ , you can output any of them.

shuffled:洗牌

试做

#include "iostream"
#include "cstring"
using namespace std;
#define shanzr main
const int N=100010;
int shanzr(){
    int t;cin>>t;
    int n,x;
    while(t--){
        cin>>n;
        while(n--)cin>>x;
        cout<<x<<endl;
    }
}

小记

这题。。还真是有意思。另外暴力法:

援引自https://www.luogu.com.cn/blog/callsign-none/solution-cf1698a

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	register int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*f;
}
void solve(){
	//do something
	int n=read(),a[105];
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<=n;i++){
		int tmp=0;
		for(int j=1;j<=n;j++){
			if(j==i) continue;
			tmp^=a[j];
		}
		if(tmp==a[i]){
			printf("%d\n",tmp);
			return;
		}
	}
}
int main(){
	int t=read();
	while(t--) solve();
}

暴力也可以稍微用一下前后缀。

红2:Promo

题目描述

The store sells $ n $ items, the price of the $ i $ -th item is $ p_i $ . The store's management is going to hold a promotion: if a customer purchases at least $ x $ items, $ y $ cheapest of them are free.

The management has not yet decided on the exact values of $ x $ and $ y $ . Therefore, they ask you to process $ q $ queries: for the given values of $ x $ and $ y $ , determine the maximum total value of items received for free, if a customer makes one purchase.

Note that all queries are independent; they don't affect the store's stock.

promotion:促销 stock:存货

试做

#include "iostream"
#include "algorithm"
using namespace std;
#define shanzr main
const int N=200010;
int num[N];
int shanzr(){
    int n,q;cin>>n>>q;
    for(int i=1;i<=n;i++)cin>>num[i];
    sort(num+1,num+n+1);
    int x,y;
    while(q--){
        cin>>x>>y;
        int sum=0;
        for(int i=n-x+1;i<=n-x+y;i++){
            sum+=num[i];
        }
        cout<<sum<<endl;
    }
}

红3:Parkway Walk

题目描述

You are walking through a parkway near your house. The parkway has $ n+1 $ benches in a row numbered from $ 1 $ to $ n+1 $ from left to right. The distance between the bench $ i $ and $ i+1 $ is $ a_i $ meters.

Initially, you have $ m $ units of energy. To walk $ 1 $ meter of distance, you spend $ 1 $ unit of your energy. You can't walk if you have no energy. Also, you can restore your energy by sitting on benches (and this is the only way to restore the energy). When you are sitting, you can restore any integer amount of energy you want (if you sit longer, you restore more energy). Note that the amount of your energy can exceed $ m $ .

Your task is to find the minimum amount of energy you have to restore (by sitting on benches) to reach the bench $ n+1 $ from the bench $ 1 $ (and end your walk).

You have to answer $ t $ independent test cases.

bench:长凳

试做

#include "iostream"
#include "algorithm"
using namespace std;
#define shanzr main
const int N=200010;
int num[N];

int shanzr(){
    int t;cin>>t;
    int n,m;
    while(t--){
        cin>>n>>m;int sum=0;
        int x;
        for(int i=1;i<=n;i++){
            cin>>x;sum+=x;
        }
        if(m>sum)cout<<0<<endl;
        else cout<<sum-m<<endl;
    }
}

红4:All Distinct

题目描述

Sho has an array $ a $ consisting of $ n $ integers. An operation consists of choosing two distinct indices $ i $ and $ j $ and removing $ a_i $ and $ a_j $ from the array.

For example, for the array $ [2, 3, 4, 2, 5] $ , Sho can choose to remove indices $ 1 $ and $ 3 $ . After this operation, the array becomes $ [3, 2, 5] $ . Note that after any operation, the length of the array is reduced by two.

After he made some operations, Sho has an array that has only distinct elements. In addition, he made operations such that the resulting array is the longest possible.

More formally, the array after Sho has made his operations respects these criteria:

Output the length of the final array.

试做

#include "iostream"
#include "algorithm"
#include "cstring"
using namespace std;
#define shanzr main
const int N=200010;
int num[N];
bool vis[N];
int shanzr(){
    int t;cin>>t;
    int n;
    while(t--){
        memset(vis,0,sizeof vis);
        cin>>n;int k=0;
        for(int i=0;i<n;i++) {
            cin >> num[i];
            if(vis[num[i]])continue;
            k++;vis[num[i]]=true;
        }
        cout<<k-((n+k)&1)<<endl;
    }
}

红5:Marathon

题目描述

You are given four distinct integers $ a $ , $ b $ , $ c $ , $ d $ .

Timur and three other people are running a marathon. The value $ a $ is the distance that Timur has run and $ b $ , $ c $ , $ d $ correspond to the distances the other three participants ran.

Output the number of participants in front of Timur.

试做

#include "iostream"
#include "algorithm"
#include "cstring"
using namespace std;
#define shanzr main
const int N=200010;
int shanzr(){
    int t;cin>>t;int cnt;
    while(t--){
        int num[5];
        cnt=0;
        for(int i=1;i<=4;i++){
            cin>>num[i];
            if(i==1)continue;
            if(num[i]>num[1])cnt++;
        }
        cout<<cnt<<endl;
    }
}

橙1:Mark the Photographer

题目描述

Mark is asked to take a group photo of $ 2n $ people. The $ i $ -th person has height $ h_i $ units.

To do so, he ordered these people into two rows, the front row and the back row, each consisting of $ n $ people. However, to ensure that everyone is seen properly, the $ j $ -th person of the back row must be at least $ x $ units taller than the $ j $ -th person of the front row for each $ j $ between $ 1 $ and $ n $ , inclusive.

Help Mark determine if this is possible.

试做

#include "iostream"
#include"algorithm"
using namespace std;
int num[1010];
int main(){
    int t;cin>>t;
    int n,x;
    while(t--){
        cin>>n>>x;
        for(int i=1;i<=2*n;i++){
            cin>>num[i];
        }
        sort(num+1,num+2*n+1);
        bool flag=true;
        for(int i=1;i<=n;i++){
            if(num[i+n]-num[i]<x){
                flag= false;
                break;
            }
        }
        if(flag)cout<<"YES\n";
        else cout<<"NO\n";
    }
}

橙2:Double Strings

题目描述

You are given $ n $ strings $ s_1, s_2, \dots, s_n $ of length at most $ \mathbf{8} $ .

For each string $ s_i $ , determine if there exist two strings $ s_j $ and $ s_k $ such that $ s_i = s_j + s_k $ . That is, $ s_i $ is the concatenation of $ s_j $ and $ s_k $ . Note that $ j $ can be equal to $ k $ .

Recall that the concatenation of strings $ s $ and $ t $ is $ s + t = s_1 s_2 \dots s_p t_1 t_2 \dots t_q $ , where $ p $ and $ q $ are the lengths of strings $ s $ and $ t $ respectively. For example, concatenation of "code" and "forces" is "codeforces".

concatenation:串联

试做

手写哈希寄了。(也可能是字符串切分搞错了

#WA代码
#include "iostream"
#include "cstring"
using namespace std;
#define shanzr main
const int N=100010;
const int MO=10007;
const int Q=1331;
string str[N];
int vis[N];
int getMp(const string&s){
    int v=0;
    for(int j=s.length()-1;j>=0;j--){
        v+=s[j]-'0';
        v*=Q;
        v%=MO;
    }
    return v;
}
int shanzr(){
    int t;cin>>t;
    while(t--){
        int n;cin>>n;
        memset(vis,0,sizeof vis);
        for(int i=1;i<=n;i++){
            cin>>str[i];
            vis[getMp(str[i])]= true;
        }
        for(int i=1;i<=n;i++){
            int flag=false;
            for(int j=1;j<str[i].size();j++){
                string a(str[i],0,j);
                string b(str[i],0+j,n-j);
                if(vis[getMp(a)]&&vis[getMp(b)]){
                    flag= true;break;
                }
            }
            if(flag)cout<<1;
            else cout<<0;
        }
        cout<<endl;
    }
    return 0;
}

\(STL\) u_map可过

#include "iostream"
#include "unordered_map"
using namespace std;
#define shanzr main
const int N=100010;
string str[N];
unordered_map<string,int> ump;
int t,n;
int shanzr()
{
    cin>>t;
    while(t--)
    {
        ump.clear();
        cin>>n;
        for(int i=1;i<=n;i++){cin>>str[i];ump[str[i]]=1;}
        for(int k=1;k<=n;k++){
            bool flag= false;
            for(int i=0;i<str[k].size();i++)
            {
                string q=str[k].substr(0,i),h=str[k].substr(i,str[k].size());
                if(ump[q]&& ump[h]){
                    flag=true;break;
                }
            }
            if(flag)cout<<1;
            else cout<<0;
        }
        cout<<endl;
    }
    return 0;
}

橙3:Cypher

题目描述

Luca has a cypher made up of a sequence of $ n $ wheels, each with a digit $ a_i $ written on it. On the $ i $ -th wheel, he made $ b_i $ moves. Each move is one of two types:

  • up move (denoted by $ \texttt{U} $ ): it increases the $ i $ -th digit by $ 1 $ . After applying the up move on $ 9 $ , it becomes $ 0 $ .
  • down move (denoted by $ \texttt{D} $ ): it decreases the $ i $ -th digit by $ 1 $ . After applying the down move on $ 0 $ , it becomes $ 9 $ .

Example for $ n=4 $ . The current sequence is 0 0 0 0.Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.

cypher:密码

试做

#include "iostream"
using namespace std;
#define shanzr main
int num[1000];
int shanzr(){
    int t;cin>>t;
    while(t--){
        int n;cin>>n;
        for(int i=1;i<=n;i++)cin>>num[i];
        for(int i=1;i<=n;i++){
            int op;cin>>op;
            while(op--){
                char c;cin>>c;
                if(c=='D'){
                    num[i]+=1;
                }
                if(c=='U'){
                    num[i]+=9;
                }
            }
            num[i]%=10;
        }
        for(int i=1;i<=n;i++)cout<<num[i]<<' ';
        cout<<endl;
    }
}

标签:shanzr,入门,题刷题,int,cin,CF,num,str,include
来源: https://www.cnblogs.com/shanzr/p/16537269.html