indeed 5.13 第二次网测
题目描述,我找不见了,大概写一下想法和代码吧。
1. 没有看
2. 由于数据范围很小,就是简单的枚举,求全排列,然后更新答案。
#include<bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e3 + ;
int n, k, m;
int a[], b[], u[];
bool ina[], inb[];
int work() {
int r = ;
for (int i = ; i < m; i++) {
if(ina[a[i] ] && ina[b[i] ])
r += u[i];
if(inb[a[i] ] && inb[b[i] ])
r += u[i];
}
return r;
}
void solve() {
cin >> n >> k >> m;
for (int i = ; i < m; i++) {
cin >> a[i] >> b[i] >> u[i];
}
vector<int> p;
for (int i = ; i <= n; i++)
p.push_back(i);
int res = ;
do {
memset(ina, , sizeof ina);
memset(inb, , sizeof inb);
for (int i = ; i < k; i++)
ina[p[i] ] = ;
for (int i = k; i < k * ; i++)
inb[p[i] ] = ;
res = max(res, work());
} while(next_permutation(p.begin(), p.end()));
cout << res << endl;
} int main() {
freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
ios::sync_with_stdio();
cin.tie(); cout.tie();
solve();
return ;
}
3. 这次第三题挺有意思的,求左上角到右下角的最短路径,但是多了一种跳的操作。
我的想法很简单,由于在每一个点上都可以选择跳与不跳,而且只能一次跳, 那就预处理出每个点到起始点的距离,每个点到终点的距离,然后遍历每一个点,更新答案。
一个是这个点到起点的距离加上到终点的距离, 一个是这个点到起点的距离 + 1(完成跳的动作) + 跳之后的点到终点的距离。更新答案。
#include<bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e3 + ;
const int inf = 1e5;
int h, w, d, r;
string a[];
int sdis[][], edis[][];
int dx[] = {, , , -};
int dy[] = {, -, , };
bool check(int x, int y) {
if(x < || x >= h || y < || y >= w) return ;
return ;
}
void dfs(int x, int y, int v) {
sdis[x][y] = v;
for (int i = ; i < ; i++) {
int cx = x + dx[i], cy = y + dy[i];
if(check(cx, cy) && a[cx][cy] != '#' && sdis[cx][cy] > v + ) {
dfs(cx, cy, v + );
}
}
}
void dfs1(int x, int y, int v) {
edis[x][y] = v;
for (int i = ; i < ; i++) {
int cx = x + dx[i], cy = y + dy[i];
if(check(cx, cy) && a[cx][cy] != '#' && edis[cx][cy] > v + ) {
dfs1(cx, cy, v + );
}
}
}
void solve() {
cin >> h >> w >> d >> r;
for (int i = ; i < h; i++)
cin >> a[i];
for (int i = ; i < h; i++) {
for (int j = ; j < w; j++)
sdis[i][j] = edis[i][j] = inf;
}
dfs(, , );
dfs1(h - , w - , );
int res = inf;
for (int i = ; i < h; i++) {
for (int j = ; j < w; j++) {
int t1 = sdis[i][j] + edis[i][j];
res = min(res, t1);
int x = i + d, y = j + r;
if(check(x, y)) {
int t2 = sdis[i][j] + edis[x][y] + ;
res = min(res, t2);
} }
}
if(res == inf) res = -;
cout << res << endl;
} int main() {
freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
ios::sync_with_stdio();
cin.tie(); cout.tie();
solve();
return ;
}
4. 目标函数是差的绝对值乘以权值之和, 考虑权值都相等的时候,这时候的最佳点就是中间的那个点(奇数个点是中间点,偶数个点是中间2个点之间的点都行)。
下面考虑权值r不相等的时候,这个函数其实是凹的, 这个性质很重要,可以进行二分查找,目标点是该点比它左右2点的函数值都要小, 如果该点比左边大,比右边小,那查找区间往左移动,否则往右移动。
#include<bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5 + ;
ll len;
int n;
ll a[maxn];
int r[maxn];
ll work(ll x) {
ll res = ;
for (int i = ; i < n; i++) {
res += abs(a[i] - x) * r[i];
}
return res;
}
void solve() {
scanf("%lld%d", &len, &n);
for (int i = ; i < n; i++) {
scanf("%lld%d", &a[i], &r[i]);
}
ll left = , right = len;
ll res = -;
while(left < right) {
ll mid = (left + right) / ;
ll r = work(mid);
ll r1 = work(mid - );
ll r2 = work(mid + );
if(r <= r1 && r <= r2) {
left = right = mid;
break;
} else if(r1 <= r && r <= r2) {
right = mid - ;
} else if(r1 >= r && r >= r2) {
left = mid + ;
}
}
if(left < ) left++;
if(left > len) left--;
printf("%lld\n", work(left));
} int main() {
freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
solve();
return ;
}
indeed 5.13 第二次网测的更多相关文章
- 2014-CVTE网测部分软件技术测试题及答案
1.叉树的先序遍历序列和后序遍历序列正好相反,则该二叉树满足的条件是(D) A.空或只有一个结点 B.高度等于其结点数 C.该二叉树是完全二叉树 D.所有结点无右孩子 应该是二叉树的每个结点都只有一个 ...
- < 独立项目 - 文本挖掘 > - 2016/11/13 第二更 - <Python环境准备>
< 独立项目 - 文本挖掘 > 项目立项的相关背景介绍,TODO方向. 一.Ubuntu环境配置 主机系统:Windows 7 SP1 64位操作系统 | i5-4210 CPU | ...
- indeed2017校招在线编程题(网测)三
A. Calculate Sequence 分析:就是斐波那契递推公式,但是初始值是指定的,只用求第10个数,数据范围和复杂度都比较小,直接写. B. 忘了叫啥了. 就是有a-j十个字符组成的字符串, ...
- wap网测一道题目
1. 给定一个字符串s, 1 <= len(s) <= 3000, 定义odd palindrome string为长度为奇数的回文串, 求s中该奇回文串的个数. 比如axbcba , 结 ...
- wap 5.23 网测几道题目
1. n个犯人,m个省份, 如果相邻的2个犯人来自同一省份,则是不安全的,求不安全的个数. 正难则反,用全部的个数减去非法的个数,就是最后的答案. m^n - m * (m - 1) ^ (n - 1 ...
- 5.27 indeed 第三次网测
1. 第一题, 没有看 2. 暴力枚举.每一个部分全排列, 然后求出最大的请求数. #include<bits/stdc++.h> #define pb push_back typedef ...
- indeed 4.22 第一次网测
1.第一题 没有看 2. 由于数据范围很小,所以每一层需要全排列,寻找最小的花费,然后所有层加起来就是最后的结果. #include<bits/stdc++.h> #define pb p ...
- PL/SQL Developer 13注册码(亲测可用)
product code: 4vkjwhfeh3ufnqnmpr9brvcuyujrx3n3le serial Number: 226959 password: xs374ca
- mysql每秒最多能插入多少条数据 ? 死磕性能压测
前段时间搞优化,最后瓶颈发现都在数据库单点上. 问DBA,给我的写入答案是在1W(机械硬盘)左右. 联想起前几天infoQ上一篇文章说他们最好的硬件写入速度在2W后也无法提高(SSD硬盘) 但这东西感 ...
随机推荐
- CAD在网页中绘制批注
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- WinForm窗体中窗口控件的生成
1:button控件的生成方式 Button button = new Button(); button.Size = new Size(80, 80); button.Location = new ...
- Python 连接数据库 day5
import pymysql #连接数据库,port必须是int型,字符编码是utf8,不能是utf-8,password必须是字符串 conn = pymysql.connect(host=', d ...
- Linux之加密(基于key认证、建立私有云CA)
对称加密: 一般的加密是用一个密码加密文件,解密用同样的密码,加密解密用一把密钥 非对称加密: 一个密码加密文件,解密却用另外一组密码,意思就是加密解密的密码不一样,其结果就是用这一组密钥中的一个来加 ...
- Codeforces Hello 2018 C - Party Lemonade
传送门:http://codeforces.com/contest/913/problem/C 有n类物品,第i(i=0,1,2,...,n-1)类物品的价值为2i,花费为ci.任意选择物品,使得总价 ...
- mysql-索引、导入、导出、备份、恢复
1.索引 索引是一种与表有关的结构,它的作用相当于书的目录,可以根据目录中的页码快速找到所需的内容. 当表中有大量记录时,若要对表进行查询,没有索引的情况是全表搜索:将所有记录一一取出,和查询条件进行 ...
- printf 打印字符串的任意一部分
使用printf()函数打印字符串的任意部分,请看下例: <span style="font-size:16px;">#include <stdio.h> ...
- 洛谷—— P1379 八数码难题
https://daniu.luogu.org/problem/show?pid=1379 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示 ...
- gap lock/next-key lock浅析 Basic-Paxos协议日志同步应用
http://www.cnblogs.com/renolei/p/4673842.html 当InnoDB在判断行锁是否冲突的时候, 除了最基本的IS/IX/S/X锁的冲突判断意外, InnoDB还将 ...
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...