Initial commit
This commit is contained in:
15
15lanqiao/test1-1.cpp
Normal file
15
15lanqiao/test1-1.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int tot = (50*49) / 2, cnt = (7*6) / 2;
|
||||
cout << tot - cnt << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
总共有50人参与会议,如果按照常规情况,即每个人与除了自己以外的所有人各握手一次
|
||||
握手总数tot=49+48+...+1=(50*49)/2
|
||||
现在有7个人没有互相握手,这7这个人本来应该握手的次数 cnt=6+5+...+1=(7*6)/2
|
||||
实际握手次数=总的握手次数tot-7人的情况cnt (tot - cnt)
|
||||
*/
|
||||
17
15lanqiao/test1-2.cpp
Normal file
17
15lanqiao/test1-2.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int ans = 0;
|
||||
for(int i = 1; i <=50; i++){
|
||||
for(int j = i + 1; j<=50; j++){
|
||||
if(!( (i >= 1 && i<= 7) && (j >= 1 && j <= 7) )) ans++;
|
||||
// i和j不能同时满足都在[1, 7]范围内
|
||||
}
|
||||
}
|
||||
cout << ans << endl;
|
||||
return 0;
|
||||
}
|
||||
/* 枚举法
|
||||
|
||||
*/
|
||||
19
15lanqiao/test2.cpp
Normal file
19
15lanqiao/test2.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int x = 343720, y = 233333, dx = 15, dy = 17;
|
||||
int p = y*dx, q = x*dy;
|
||||
int g = gcd(p, q);
|
||||
p /= g, q /= g;
|
||||
int t = 2*p*x / dx;
|
||||
double ans = t * (sqrt(15*15 + 17*17));
|
||||
printf("%.2lf\n", ans);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
思路:针对前进的方向进行分解,分为x方向的运动和y方向的运动假设x方向走了p个来回,y反向走了q个来回,
|
||||
经过了时间t,小球第一次回到原点则t*dx=2px,t*dy=2qy,结合1式/2式,得p/q=y/x*dx/dy
|
||||
利用gcd对分式p,q进行约分,进而得到约分后的p,q
|
||||
则时间 t = 2px/dx, 总路程 = t * sqrt(15*15 + 17*17)
|
||||
*/
|
||||
30
15lanqiao/test3.cpp
Normal file
30
15lanqiao/test3.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
bool check(int n){
|
||||
int b = 1; //数位
|
||||
while(n){
|
||||
if(b % 2){ // 奇数位
|
||||
if(!(n % 10 % 2)) return false;
|
||||
}else{ // 偶数位
|
||||
if(n % 10 % 2) return false;
|
||||
}
|
||||
n /= 10;
|
||||
b++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n; cin >>n;
|
||||
int ans = 0;
|
||||
for(int i = 1; i <= n; i++){
|
||||
if(i % 10 % 2 == 0) continue;
|
||||
if(check(i)) ans++;
|
||||
}
|
||||
cout << ans << endl;
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
一个整数如果按从低位到高位的顺序,奇数位为奇数,偶数位为偶数,则该数为好数
|
||||
24 -> 7, 2024 -> 150
|
||||
*/
|
||||
34
15lanqiao/test4.cpp
Normal file
34
15lanqiao/test4.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int N = 2e3 + 10;
|
||||
string s; int a[N];
|
||||
int main() {
|
||||
int n; cin >> n >> s;
|
||||
reverse(s.begin(), s.end());
|
||||
int pos = s.find("."); // 小数点位置
|
||||
s.erase(pos, 1); // 删除小数点, 方便后续计算
|
||||
int len = s.size();
|
||||
for(int i = 0; i < len; i++) a[i+1] = s[i] - '0';
|
||||
// a[1] = 4,a[2] = 1,a[3] = 3
|
||||
|
||||
//高精度*低精度模版
|
||||
for(int i = 1; i<=n; i++){
|
||||
// 顺序扫描每一位, 均乘2
|
||||
for(int j = 1; j <= len; j++) a[j] *= 2;
|
||||
for(int j = 1; j <= len; j++){
|
||||
if(a[j] > 10){
|
||||
a[j+1]++;
|
||||
a[j] %= 10;
|
||||
if(j == len) len++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// a[1] = 6,a[2] = 5,a[3] = 2,a[4] = 1 a[pos] = 5
|
||||
if(a[pos] >= 5) a[pos+1]++;
|
||||
for(int i = len; i >= pos + 1; i--) cout << a[i];
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
2 3.14
|
||||
*/
|
||||
45
15lanqiao/test5.cpp
Normal file
45
15lanqiao/test5.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int N = 1e5 + 10;
|
||||
int a[N];
|
||||
vector<int> fac[N], s[N];
|
||||
|
||||
int main() {
|
||||
for(int i = 1; i <= 1e5; i++){
|
||||
for(int j = i; j <= 1e5; j += i){
|
||||
fac[j].push_back(i);
|
||||
}
|
||||
}
|
||||
int n; cin>>n;
|
||||
for(int i = 1; i<=n; i++) cin >> a[i];
|
||||
// 保证字典序最小
|
||||
sort(a+1, a+1+n);
|
||||
for(int i = 1; i <=n; i++){
|
||||
for(int j = 0; j < fac[a[i]].size(); j++){
|
||||
s[fac[a[i]][j]].push_back(a[i]);
|
||||
}
|
||||
}
|
||||
for(int i = 1e5; i>=0; i--){
|
||||
if(s[i].size() >= 3){
|
||||
cout << s[i][0] << " " << s[i][1] << " " << s[i][2] << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/* test samples
|
||||
5
|
||||
1 2 3 4 9
|
||||
*/
|
||||
|
||||
/*
|
||||
排序后的宝石为 [1, 2, 3, 4, 9]。
|
||||
因数数组 fac 和宝石数组 s 的关系如下:
|
||||
s[1] 包含所有宝石 [1, 2, 3, 4, 9]。
|
||||
s[2] 包含 [2, 4]。
|
||||
s[3] 包含 [3, 9]。
|
||||
s[4] 包含 [4]。
|
||||
s[9] 包含 [9]。
|
||||
最终,s[1] 中有5个宝石,满足条件。
|
||||
输出前三个宝石:1 2 3。
|
||||
*/
|
||||
57
15lanqiao/test6.cpp
Normal file
57
15lanqiao/test6.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int N = 20;
|
||||
int a[N][N]; // 迷宫数组
|
||||
int vis[N][N]; // 标记数组
|
||||
int n, k;
|
||||
// 方向数组 0 1 2 3 4 5 6 7
|
||||
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
|
||||
int dy[] = { 0, 1, 1, 1, 0, -1, -1, -1};
|
||||
string res; // 结果字符串
|
||||
|
||||
void dfs(int x, int y, int pre, string s, int dep){
|
||||
if(x == n && y == n && dep == n*n){
|
||||
if(res.empty()) res = s; // 字典需最小的
|
||||
return;
|
||||
}
|
||||
for(int i = 0; i < 8; i++){
|
||||
int bx = x + dx[i], by = y + dy[i];
|
||||
if(bx < 1 || bx > n || by < 1 || by > n) continue;
|
||||
if(vis[bx][by]) continue;
|
||||
// 防止交叉搜索
|
||||
if(i == 1 && vis[x-1][y] && vis[x][y+1]) continue;
|
||||
else if(i == 3 && vis[x+1][y] && vis[x][y+1]) continue;
|
||||
else if(i == 5 && vis[x+1][y] && vis[x][y-1]) continue;
|
||||
else if(i == 7 && vis[x-1][y] && vis[x][y-1]) continue;
|
||||
// 保证路径数值为0 1 ... k-1
|
||||
if((a[bx][by] < k && a[bx][by] == pre + 1) || (pre + 1 == k && a[bx][by] == 0)){
|
||||
vis[bx][by] = 1;
|
||||
dfs(bx, by, a[bx][by], s + to_string(i), dep + 1);
|
||||
// 最优性剪枝
|
||||
if(!res.empty()) return;
|
||||
vis[bx][by] = 0; //回溯
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n >> k;
|
||||
for(int i = 1; i <= n; i++){
|
||||
for(int j = 1; j <= n; j++){
|
||||
cin >> a[i][j];
|
||||
}
|
||||
}
|
||||
string emp;
|
||||
vis[1][1] = 1;
|
||||
dfs(1, 1, 0, emp, 1);
|
||||
if(res.empty()) cout << -1 << endl;
|
||||
else cout << res << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* test samples
|
||||
3 3
|
||||
0 2 0
|
||||
1 1 1
|
||||
2 0 2
|
||||
*/
|
||||
26
15lanqiao/test7.cpp
Normal file
26
15lanqiao/test7.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int N = 1e5 + 10;
|
||||
int main(){
|
||||
priority_queue<int> pq; // 大根堆
|
||||
int n, p ,q; cin >> n >> p >> q;
|
||||
for(int i = 1; i <= n; i++){
|
||||
int x; cin >> x;
|
||||
pq.push(x);
|
||||
}
|
||||
while(p || q){
|
||||
int cur = pq.top(); // 最大值
|
||||
pq.pop();
|
||||
if(p){ cur = sqrt(cur); p--; }
|
||||
else if(q) { cur = cur / 2; q--; }
|
||||
pq.push(cur);
|
||||
}
|
||||
int s = 0; // 总的消耗
|
||||
while(!pq.empty()) { s += pq.top(); pq.pop(); }
|
||||
cout << s << endl;
|
||||
return 0;
|
||||
}
|
||||
/* test samples
|
||||
4 1 1
|
||||
4 5 6 49
|
||||
*/
|
||||
44
15lanqiao/test8.cpp
Normal file
44
15lanqiao/test8.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
#define int long long
|
||||
const int N = 1e3+10;
|
||||
int a[N], s[N]; // s为前缀和数组
|
||||
multiset<int> ms;
|
||||
|
||||
signed main(){
|
||||
int n; cin>>n;
|
||||
for(int i = 1; i<=n; i++){
|
||||
cin >> a[i];
|
||||
s[i] = s[i-1] + a[i];
|
||||
}
|
||||
// 使用set数组去维护所有的区间和
|
||||
for(int i = 1; i<=n; i++){
|
||||
for(int j = 1; j<=n; j++){
|
||||
ms.insert(s[j] - s[i-1]);
|
||||
}
|
||||
}
|
||||
int ans = LLONG_MAX;
|
||||
|
||||
// 时间复杂度为 O(n^log2n)
|
||||
for(int i = 1; i<=n; i++){
|
||||
for(int j = 1; j<i; j++){
|
||||
// 枚举以i结尾的区间
|
||||
int sum = s[i] - s[j-1];
|
||||
// 找到该区间和sum相似的区间和s1 and s2
|
||||
auto it = ms.lower_bound(sum);
|
||||
if(it!=ms.end()){ ans = min(ans, abs(*it - sum)); } // 找到了
|
||||
if(it!=ms.begin()){
|
||||
it--;
|
||||
ans = min(ans, abs(*it - sum));
|
||||
}
|
||||
}
|
||||
// 删除以i开头且以j结尾的区间,防止后续查询区间的时候出现区间重叠/交叉重复问题
|
||||
for(int j = i; j<=n; j++) ms.erase(ms.find(s[j] - s[i-1]));
|
||||
}
|
||||
cout << ans << endl;
|
||||
return 0;
|
||||
}
|
||||
/* test samples
|
||||
5
|
||||
10 9 8 12 14
|
||||
*/
|
||||
Reference in New Issue
Block a user