当前位置:网站首页>Niuke novice monthly race 40
Niuke novice monthly race 40
2022-07-06 11:25:00 【%xiao Q】
A. Digital games
A sign in question , Just imitate the meaning of the question , Pay attention to input and output , Card input , Output , It is best to scanf,printf, Do not use cin,cout
Reference code ;
#include <iostream>
#include <cstdio>
using namespace std;
#define lowbit(x) (x & -x)
int main()
{
int T;
cin >> T;
while(T--)
{
int x;
scanf("%d", &x);
int ans = 0;
while(x)
{
int cnt = 0, t, dx = x;
// use lowbit Calculate how many 1, And find the highest one 1 Number expressed in decimal system of , use t Record
while(dx)
{
if(dx == lowbit(dx)) t = dx;
cnt++;
dx -= lowbit(dx);
}
if(cnt % 2) x ^= 1; // Take the last digit in reverse
else x -= t; // Because the highest position must be 1, So just subtract t
ans++;
}
printf("%d\n", ans);
}
return 0;
}
B. Jump, jump, jump
Algorithm : Circular interval dp
First we have to deal with a ring , We can drive 2 Double space , Make the first half the same as the second half , And then in the interval dp, See the code
#include <iostream>
using namespace std;
const int N = 2010;
int a[2 * N];
int f[2 * N][2 * N]; //f[i][j] From i Jump to the j Need to consume
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i], a[i + n] = a[i];
for(int len = 1; len <= n; len++)
for(int i = 1; i <= 2 * n; i++)
{
int j = i + len - 1;
f[i][j] = max(f[i][j], f[i][j - 1] + len * a[j]); // Jump right
f[i][j] = max(f[i][j], f[i + 1][j] + len * a[i]); // Jump left
}
int ans = 0;
for(int i = 1; i <= n; i++)
ans = max(ans, f[i][i + n - 1]);
cout << ans << endl;
}
C. Number matching
This question , We can drive 2 Layer cycle to violence , In judging this 2 Number x, y Whether the conditions are met , The key is how we have to judge ?
here , We can find out x,y The length of binary expression is k Number of numbers , Convert to decimal , And write it down , see x,y Whether there is the same , Of course, the length is greater than k It must be possible , Because the length is greater than k The existence of , So the length is k It must exist .
Reference code :
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 2010;
int n, k;
bool st[N];
// Used to calculate the length k The decimal system of numbers
int f(int x)
{
return x & ((1 << k) - 1);
}
bool judge(int x, int y)
{
memset(st, 0, sizeof st);
int t = 1 << (k - 1);
// Less than t It must be impossible to match successfully
while(x >= t)
{
st[f(x)] = true; // Make a mark
x >>= 1;
}
while(y >= t)
{
if(st[f(y)]) return true;
y >>= 1;
}
return false;
}
int main()
{
scanf("%d%d", &n, &k);
int ans = 0;
for(int i = 1; i < n; i++)
for(int j = i + 1; j <= n; j++)
if(judge(i, j)) ans++;
printf("%d\n", ans);
return 0;
}
D. Beautiful string
A water problem , It's not specific , See the code
Reference code :
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
while(T--)
{
string s;
cin >> s;
int ans = 0, t = 1; char ch = s[0];
for(int i = 1; i < s.size(); i++)
{
if(s[i] == ch) t++;
else
{
// cout << t << ' ';
ans += t - 1;
t = 1, ch = s[i];
}
}
ans += t - 1;
cout << ans + s.size() << endl;
}
return 0;
}
E. grouping
A question with a two point answer , See code for details .
Reference code :
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int a[N];
bool judge(int x)
{
int sum = 0, t = 1;
for(int i = 1; i < n; i++)
{
if(a[i] != a[i + 1] || t == x)
{
t = 1;
sum++;
}
else t++;
}
if(sum > m) return false;
return true;
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int ans = -1;
int l = 1, r = n;
while(l < r)
{
int mid = l + r >> 1;
if(judge(mid))
{
r = mid;
ans = mid;
}
else l = mid + 1;
}
cout << ans << endl;
return 0;
}
F. cross the bridge
A linear dp topic , It's easy to write the state transition equation , See the code
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 2010;
int a[N];
int f[N]; // f[i] Means to transfer to i Minimum time for floating blocks
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
memset(f, 0x3f, sizeof f);
f[1] = 0;
for(int i = 1; i <= n; i++)
{
if(a[i] < 0)
{
for(int j = 1; j <= i + a[i]; j++)
f[j] = min(f[j], f[i] + 1);
}
else
{
for(int j = i + 1; j <= i + a[i]; j++)
f[j] = min(f[j], f[i] + 1);
}
}
if(f[n] == 0x3f3f3f3f) puts("-1");
else cout << f[n] << endl;
return 0;
}
G. Air conditioning remote control
Algorithm : Double pointer + greedy
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int a[N];
int main()
{
int n, p;
cin >> n >> p;
for(int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int ans = -1, l = 0, r = 0;
for(int i = p + a[0]; i <= a[n - 1] - p; i++)
{
while(r < n && a[r] - i < p + 1) r++;
while(l < r && i - a[l] > p) l++;
ans = max(ans, r - l);
}
cout << ans << endl;
return 0;
}
There is also a way to write the prefix and , I won't write
H. Have some gcd
This question , We need to judge x Compliance , We can find out that all in the set are x The number of multiples of , And take them gcd, See if it's on x equal , Because we need to find out the relationship with x equal gcd(a1, a2, a3, …), These numbers must be x Multiple ,
Reference code :
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int a[N], g[N], cnt[N]; // g[i] Deposit is 1~n Each number in the set exists i Multiple of gcd
int main()
{
int T;
cin >> T;
while(T--)
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) cnt[i] = g[i] = 0;
for(int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
cnt[x]++;
}
for(int i = 1; i <= n; i++)
for(int j = i; j <= n; j += i)
if(cnt[j]) g[i] = __gcd(g[i], j);
while(m--)
{
int x;
scanf("%d", &x);
if(g[x] == x) puts("YES");
else puts("NO");
}
}
return 0;
}
I. Gymnastics formation
Algorithm :dfs
A full permutation problem , Just one more limitation ,
Reference code :
#include <iostream>
using namespace std;
int n, ans;
int a[20];
bool st[20];
void dfs(int u)
{
if(u == n + 1)
{
ans++;
return ;
}
for(int i = 1; i <= n; i++)
{
if(!st[i] && !st[a[i]]) // st[a[i]] Express a[i] Has this number ever appeared before , It's true , There have been , For false , Has never appeared
{
st[i] = true;
dfs(u + 1);
st[i] = false;
}
}
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
dfs(1);
cout << ans << endl;
}
边栏推荐
- One click extraction of tables in PDF
- How to set up voice recognition on the computer with shortcut keys
- MySQL与c语言连接(vs2019版)
- Summary of numpy installation problems
- AcWing 1298.曹冲养猪 题解
- [download app for free]ineukernel OCR image data recognition and acquisition principle and product application
- Armv8-a programming guide MMU (2)
- Codeforces Round #753 (Div. 3)
- QT creator support platform
- AcWing 179.阶乘分解 题解
猜你喜欢
Windows下安装MongDB教程、Redis教程
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
Learn winpwn (2) -- GS protection from scratch
[number theory] divisor
学习问题1:127.0.0.1拒绝了我们的访问
Redis的基础使用
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
自动机器学习框架介绍与使用(flaml、h2o)
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
随机推荐
Ansible实战系列一 _ 入门
JDBC principle
Introduction and use of automatic machine learning framework (flaml, H2O)
Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
Install MySQL for Ubuntu 20.04
Valentine's Day flirting with girls to force a small way, one can learn
Remember a company interview question: merge ordered arrays
机器学习--人口普查数据分析
Attention apply personal understanding to images
Software testing and quality learning notes 3 -- white box testing
[number theory] divisor
MySQL master-slave replication, read-write separation
Integration test practice (1) theoretical basis
UDS learning notes on fault codes (0x19 and 0x14 services)
Case analysis of data inconsistency caused by Pt OSC table change
Did you forget to register or load this tag 报错解决方法
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
Swagger, Yapi interface management service_ SE
Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
Ansible实战系列三 _ task常用命令