菜鸡也就只配打打这种难度的比赛o(╥﹏╥)o
点这—>题目传送门
A题:Directional Move(模拟)
题目大意:一个人初始是面向东边,现在给你一个二进制字符串,如果当前字符串是‘0’,则他向右转90°,如果是‘1’,则向左转90°,问最终它朝向哪?
题目思路:直接模拟,我们可以设:0东,1南,2西,3北
右转就是当前 (当前位置数 + 1)%4,向左转就是 (当前位置数+3)%4,输出最终结果就可以
代码:
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef __int64 bi;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
// 0东,1南,2西,3北
signed main(void)
{
int t,n;
t = read();
map<int,char> m;
char s[maxn];
m[0]='E';m[1]='S';m[2]='W';m[3]='N';
while(t--){
n = read();
scanf("%s",s);
// 初始是朝向东
int res = 0;
for(int i=0;i<n;++i){
if(s[i]=='0') res = (res + 1) % 4;
else res = (res + 3) % 4;
}
cout<<m[res]<<endl;
}
}
B题:Make All Odd(思维)
题目大意:给你一个数组,你可以将任意位置的两个数想加合并,问该数组是否可以经过若干次数合并使得所有元素都是奇数,如果能输出最少需要合并多少次,如果不能输出 - 1
题目思路:简单的数学思维,我们知道只有 奇数 + 偶数 = 奇数,所以如果数组全为偶数,则不可能合并出奇数,如果偶数有m个,我们可以用1个奇数多m个偶数一一相加合并,所以最小操作数应该是偶数的个数
代码:
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef __int64 bi;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
int a[maxn];
signed main(void)
{
int t,n;
t = read();
while(t--){
n = read();
int odd = 0;
for(int i=1,an;i<=n;++i){
an = read();
if(an % 2 != 0) ++odd;
}
if(odd == 0) cout<<"-1"<<endl;
else cout<<n-odd<<endl;
}
}
C题:Team(排序 + 二分 + 贪心)
题目大意:给你一个数组,第i位元素代表第i个学生擅长的技能数,现在老师想让这些学生分组,条件如下:一个学生只能在一个组或者不参加任何一个组,一个组的人数最少一人,最多两人,这个组的技能数等于该组所有学生的技能数之和,每个组的技能数至少为k,问你如何分组可使得组数尽可能多
题目思路:首先将数组存入set中,因为set是默认有序的,根据贪心算法,将set中大于等于k的元素单独计数,然后从前面小的元素第一个位置开始遍历,每次在后面找一个元素,使得这两个元素之和大于等于 k,同时为了防止重复计数,需要将这两个元素从set中删除。如果我们在查找这块使用暴力的话,是会超时的,所以可以使用set的二分函数来进行,所以总的时间复杂度应该是 O(nlogn)
代码:
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef __int64 bi;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
signed main(void)
{
//cout << "你好" << endl;
int t,n,k;
t = read();
while(t--){
n = read();k = read();
multiset<int> s;
for(int i=1,x;i<=n;++i){
x = read();
s.insert(x);
}
int cnt = 0;
while(s.size()){
int left = *s.begin();
s.erase(s.begin());
if(left >= k)
++cnt;
else{
int right = *s.lower_bound(k-left);//二分查找
if(!s.count(right)) continue; //也就是right不存在
s.erase(s.find(right));
if(left + right >= k) ++cnt;
}
}
cout << cnt << endl;
}
}
D题:XOR Game(思维 + 博弈)
题目大意:Alice和Bob玩游戏,Alice手里有两个数a和b,Bob可以选择两个数c和d,条件是 1 <= c <= a,1 <= d <= b,如果能找到两个数c和d使得c和d的异或大于a和b的异或,则Bob胜利,否则Alice胜,如果能找到这样子的c和d,输出Yes,否则输出No
题目思路:观察a和b的二进制位串,如果a和b二进制位串同位的地方存在都为1的情况,则一定是Bob胜利,否则Bob必败,可以使用bitset获取一个数的二进制位串,然后判断就可
代码:
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef __int64 bi;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
signed main(void)
{
// 使用bitset快速获取二进制位串
int t,a,b;
t = read();
while(t--){
a = read();b = read();
bitset<32> aa(a);
bitset<32> bb(b);
// 如果位串里面位同一,则输出yes,否则输出no
bool is = false;
for(int i=0;i<aa.size();++i){
if(aa[i] == 1 && aa[i] == bb[i]){
is = true;
break;
}
}
if(is) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
E题:Password(数学 + 动态规划)
题目大意:给你一个字符串,字符串仅由1-9和‘-’号组成,‘-’号可以替换为1-9的任意数,但是你必须确保你将‘-’处填为数字的时候,整个数组字符串是非递减,问最终符合条件的字符串有多少种,如果不存在就输出0
题目思路:一开始以为要用费马小定理逆元求组合数来解,发现公式弄不出来(菜昂),后面试了试动态规划,还是过了,O(∩_∩)O哈哈~,动态规划真是妙极了!,如果数组中存在不符合非递减的子串,那整体肯定不可能是非递减的串,所有输出0
设 字 符 串 为 s 用 d p [ i ] [ d i g i t ] 表 示 当 前 子 串 长 度 为 i 时 , 而 且 s [ i ] = d i g i t , 初 始 化 一 些 基 本 数 值 , d p [ 0 ] [ i ] = 1 , i ∈ [ 1 , 9 ] 下 面 分 两 种 情 况 来 进 行 讨 论 : ① 如 果 s [ i ] = ′ − ′ , 我 们 可 以 通 过 状 态 转 移 将 数 组 累 加 位 , 也 就 是 d p [ i ] [ d i g i t ] = d p [ i ] [ 1 ] + d p [ i ] [ 2 ] + . . . d p [ i ] [ d i g i t ] ② 否 则 , 我 们 将 状 态 进 行 传 递 d p [ i ] [ d i g i t ] = d p [ i ] [ 1 ] + d p [ i ] [ 2 ] + . . . d p [ i ] [ d i g i t ] 累 加 结 果 到 高 位 : d p [ i ] [ d i g i t ] + = d p [ i − 1 ] [ d i g i t ] 其 中 d i g i t ∈ [ 2 , 9 ] 最 终 输 出 d p [ n ] [ 9 ] 就 是 结 果 了 设字符串为s\\ 用dp[i][digit]表示当前子串长度为i时,而且s[i]=digit,\\ 初始化一些基本数值,dp[0][i] = 1,i∈[1,9]\\ 下面分两种情况来进行讨论:\\ ①如果s[i]='-',我们可以通过状态转移将数组累加\\ 位,也就是dp[i][digit]=dp[i][1]+dp[i][2]+...dp[i][digit]\\ ②否则,我们将状态进行传递dp[i][digit]=dp[i][1]\\+dp[i][2]+...dp[i][digit]\\ 累加结果到高位:dp[i][digit] += dp[i-1][digit]\\ 其中 digit∈[2,9]\\ 最终输出dp[n][9]就是结果了 设字符串为s用dp[i][digit]表示当前子串长度为i时,而且s[i]=digit,初始化一些基本数值,dp[0][i]=1,i∈[1,9]下面分两种情况来进行讨论:①如果s[i]=′−′,我们可以通过状态转移将数组累加位,也就是dp[i][digit]=dp[i][1]+dp[i][2]+...dp[i][digit]②否则,我们将状态进行传递dp[i][digit]=dp[i][1]+dp[i][2]+...dp[i][digit]累加结果到高位:dp[i][digit]+=dp[i−1][digit]其中digit∈[2,9]最终输出dp[n][9]就是结果了
代码:
//#include<bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <vector>
#include <queue>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef __int64 bi;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
ll dp[maxn][10];
char s[maxn];
int n;
void debug(){
for(int i = 0;i<=n;++i){
for(int j=1;j<=9;++j){
printf("%-4d",dp[i][j]);
}
cout<<endl;
}
}
signed main(void)
{
int t;
t = read();
while(t--){
n = read();
scanf("%s",s+1);
ll ans = 0;
// 初始化
for(int i=1;i<=n;++i)
for(int j=0;j<=9;++j)
dp[i][j] = 0;
for(int i=1;i<=9;++i)
dp[0][i] = 1;
for(int i=1;i<=n;++i){
// 如果 s[i]是数字,只取前一个状态不超过s[i]的数
if(isdigit(s[i]))
dp[i][s[i] - '0'] += dp[i-1][s[i] - '0'];
else{
for(int j = 1;j <= 9;++j)
dp[i][j] = (dp[i][j] + dp[i-1][j])%mod;
}
// 累加到最高位
for(int j=2;j <= 9;++j)
dp[i][j] = (dp[i][j] + dp[i][j-1])%mod;
}
//debug();
cout<< dp[n][9] <<endl;
}
}
F题:Offer(双指针 + 滑动窗口)
题目大意:有一个人去商店买东西,它只带了k元,商场给你一个商品价格的数组a,第i个商品的价格是a[i],但这个人有些怪,它会选一个区间[l,r],买a[l], a[l+1], … a[r]这些商品,商店看它如此奇怪,所以给了这个人一个特权,它购买的区间内如果存在两个商品价格是一样的,除了第一次买需要,后面就直接免费,例如[1,1,2,2,2,1],加入这个是他要买的区间,则这个人只需要付(1+2)元就可以买到6个商品,现在问你他拿k元最多可以买到多少商品
题目思路:典型的双指针+滑动窗口题目,暴力匹配肯定超时,我们维护一个长度不断变大,但是该其内的商品价格之和不超过k的滑动窗口,如果此时商品个数大于滑动窗口长度,就更新滑动窗口长度,最终输出滑动窗口长度就可以了
代码:
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef __int64 bi;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
// curf数组用于统计不同价格商品的个数
int curf[10000006];
signed main(void)
{
int t,n,k;
t = read();
while(t--){
n = read();k = read();
vector<int> a(n);
for(int i=0;i<n;i++) a[i] = read(),curf[a[i]]=0;
int cost = 0, ans = 0;
int l = 0;
for(int r = 0; r < n; r++)
{
if(curf[a[r]] == 0)
cost += a[r];
curf[a[r]]++;
while(cost > k)
{
curf[a[l]]--;
if(curf[a[l]] == 0)
cost -= a[l];
l++;
}
ans = max(ans, r - l + 1);
}
cout<<ans<<endl;
}
}