Codeforces Round #374 (Div. 2)
A题和B题是一如既往的签到题。
C题是一道拓扑序dp题,题意是给定一个DAG,问你从1号点走到n号点,在长度不超过T的情况下,要求经过的点数最多,换个思维,设dp[i][j]表示到i号点时经过j个点的最小距离,我们按拓扑序转移即可,最后找到一个最大的x,使得dp[n][x]<=T即可,由于还要输出路径,我们就需要记录一下转移。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=;
typedef long long int64;
int n,m,T,tot,now[maxn],stack[maxn],top,fa[maxn][maxn],prep[maxn],son[maxn],val[maxn],head,tail,list[maxn<<][];
int dist[maxn][maxn];
void add(int u,int v,int w){tot++,prep[tot]=now[u],now[u]=tot,son[tot]=v,val[tot]=w;}
int main(){
scanf("%d%d%d",&n,&m,&T);
tot=,memset(now,,sizeof(now));
for (int u,v,w,i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
memset(dist,,sizeof(dist)); dist[][]=;
head=,tail=,list[][]=,list[][]=; int y,z;
while (head!=tail){
head++; if (head==) head=;
y=list[head][],z=list[head][];
for (int i=now[y],so=son[i];i;i=prep[i],so=son[i]){
if (dist[so][z+]>dist[y][z]+val[i]){
dist[so][z+]=dist[y][z]+val[i];
fa[so][z+]=y;
tail++; if (tail==) tail=;
list[tail][]=so,list[tail][]=z+;
}
}
}
int pos;
for (int i=n;;i--){if (dist[n][i]<=T){pos=i;break;}}
printf("%d\n",pos); y=n; top=;
while (pos){
stack[++top]=y;
y=fa[y][pos],pos--;
}
for (int i=top;i>=;i--) printf("%d ",stack[i]);
puts("");
return ;
}
D题是一个贪心题,题意是给你一个序列,最多进行k次操作,每次操作是将某个位置上的数+x或-x,要求输出若干次操作后这个序列变成什么样子,并且要求这个序列中所有元素的乘积最小。
做法:我们先肯定是要把乘积变为负数,怎么变呢,如果是正数,我们要找到绝对值最小的那个数,如果小于0,就一直+x,直到变成>0,否则就一直-x,直到变成<0.之后我们的目标就是让某些数绝对值变大,因为这样才能尽可能小,还是一样的,每次选择绝对值最小的数,把它绝对值变大,这个过程用个堆高效地维护即可,复杂度nlogn.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define PI pair<long long,int>
#define mp(a,b) make_pair(a,b)
using namespace std;
typedef long long int64;
const int maxn=;
int n,k,op;
int64 x,a[maxn],t1,t2;
priority_queue < PI,vector<PI>,greater<PI> > heap;
int main(){
scanf("%d%d%I64d",&n,&k,&x); op=;
for (int i=;i<=n;i++) scanf("%I64d",&a[i]);
for (int i=;i<=n;i++) if (a[i]<) op=-op;
if (op==){
t1=1LL*maxn*maxn;
for (int i=;i<=n;i++) if (abs(a[i])<abs(t1)) t1=a[i],t2=i;
if (t1<){
while (k&&t1<=){
k--,t1+=x;
}
a[t2]=t1;
}else{
while (k&&t1>=){
k--,t1-=x;
}
a[t2]=t1;
}
}
if (k){
while (!heap.empty()) heap.pop();
for (int i=;i<=n;i++) heap.push(mp(abs(a[i]),i));
while (k--){
t1=heap.top().first,t2=heap.top().second; heap.pop();
if (a[t2]<) a[t2]-=x;
else a[t2]+=x;
heap.push(mp(abs(a[t2]),t2));
}
}
for (int i=;i<=n;i++) printf("%I64d ",a[i]);
puts("");
return ;
}
E题是一个dp题,还不会写,待填坑......
Codeforces Round #374 (Div. 2)的更多相关文章
- 拓扑序+dp Codeforces Round #374 (Div. 2) C
http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- Codeforces Round #374 (Div. 2) B. Passwords 贪心
B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...
- Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crosswor 水题
A. One-dimensional Japanese Crossword 题目连接: http://codeforces.com/contest/721/problem/A Description ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
题目链接:http://codeforces.com/problemset/problem/721/D D. Maxim and Array time limit per test 2 seconds ...
- Codeforces Round #374 (Div. 2) C. Journey —— DP
题目链接:http://codeforces.com/contest/721/problem/C C. Journey time limit per test 3 seconds memory lim ...
- Codeforces Round #374 (Div. 2) B. Passwords —— 基础题
题目链接:http://codeforces.com/contest/721/problem/B B. Passwords time limit per test 2 seconds memory l ...
- Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crossword —— 基础题
题目链接:http://codeforces.com/contest/721/problem/A A. One-dimensional Japanese Crossword time limit pe ...
随机推荐
- Scrum vs. PMP vs. PRINCE2的发展趋势图
这时2013年来自Google Trends的两幅图,数据来自对“Jobs and Education”的统计,体现了这三种认证,或者视为三种项目实施方式的趋势. 下图是全球的趋势: 下图是美国的趋势 ...
- [AlwaysOn Availability Groups]排查:Primary上的修改无法在Secondary体现
排查:Primary上的修改无法在Secondary体现 客户端进程在primary上修改成功,但是在Secondary上却无法看到修改结果.这个case假设你的可用性组有同步的健康问题.很多情况下这 ...
- js中操作数组的一些方法
增 push 在数组的末尾添加一个或多个元素,并返回新的长度. array.push(1,2,3.........) unshift 在数组的开头添加一个或多个元素,并返回新的长度. arra ...
- win7升win10,初体验
跟宿舍哥们聊着聊着,聊到最近发布正式版的win10,听网上各种评论,吐槽,撒花的,想想,倒不如自己升级一下看看,反正不喜欢还可以还原.于是就开始了win10的初体验了,像之前装黑苹果双系统一样的兴奋, ...
- 【转载】4412开发板嵌入式QtE应用开发环境搭建
本文转自迅为iTOP-4412开发板实战教程书籍:http://topeetboard.com QtE应用需要使用开发工具qtcreator,本文介绍qtcreator-3.2.2的安装和使用.1. ...
- [WPF系列]-高级部分 需要区分的东东
ContentControl VS ContentPresenter What's the difference between ContentControl and ContentPresenter ...
- NYOJ---540奇怪的排序
奇怪的排序 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 最近,Dr. Kong 新设计一个机器人Bill.这台机器人很聪明,会做许多事情.惟独对自然数的理解与人类不一 ...
- Android USB Gadget复合设备驱动(打印机)测试方法
启动Android打印机设备,并用USB线连接电脑主机及Android打印机. Android打印机系统启动完成后,在Windows设备管理器中,可以看到Android Phone设备和USB打印支持 ...
- 装13失败后的逆袭(ComboBox的联动)
当我们在做ComboBox的联动的时候飞一般的敲出自认为完美的代码.在运行的时候突然变得不完美了. 比如: 如果发生了这种情况会不会就卡磁了呢 当然不会作为程序猿的我们考的是我们聪明的大脑,当然会想出 ...
- 第3章 Linux常用命令(1)_文件处理命令
1. 文件处理命令 1.1 命令格式:命令 [-option] [arguments],如ls –la /etc (1)个别命令使用不遵循此格式 (2)当有多个选项时,可以写在一起,如以上的-la ( ...