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 ...
随机推荐
- 在 CentOS7 上安装 MongoDB
在 CentOS7 上安装 MongoDB 1 通过 SecureCRT 连接至 CentOS7 服务器: 2 进入到 /usr/local/ 目录: cd /usr/local 3 在当前目录下创建 ...
- 怎样在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块
因为项目的需要,所以研究了一下自动生成测试代码.将经验记录下来,总会有用的.我个人认为,好记性不如多做笔记多反思总结. 1. 前提条件 开发环境已正确配置 工程已解决JUnit依赖关系(pom ...
- C#实现在图片上斜着写字
最近公司要搞微信活动页面,要实现图片上可以写自己名字的功能,于是就查了一下怎么实现,下面贴一下代码备忘,希望大家也能用到: 我是在控制台应用程序里进行试验的. using (Image bitmap ...
- asp.net signalR 专题—— 第三篇 如何从外部线程访问 PersistentConnection
在前面的两篇文章中,我们讲到的都是如何将消息从server推向client,又或者是client再推向server,貌似这样的逻辑没什么异常,但是放在真实 的环境中,你会很快发现有一个新需求,如何根据 ...
- Flume组件source,channel,sink源码分析
LifeCycleState: IDLE, START, STOP, ERROR [Source]: org.apache.flume.Source 继承LifeCycleAware{stop() + ...
- .NET应用架构设计—面向对象分析与设计四色原型模式(彩色建模、领域无关模型)(概念版)
阅读目录: 1.背景介绍 2.问自己,UML对你来说有意义吗?它帮助过你对系统进行分析.建模吗? 3.一直以来其实我们被一个缝隙隔开了,使我们对OOAD遥不可及 4.四色原型模式填补这个历史缝隙,让我 ...
- 实时事件统计项目:优化solr和morphline的时间字段
morphline优化,如下: 传过来的时间戳被复制到3个字段:eventTimeInMinuteChina_tdt ,eventTimeInMinuteUTC_tdt ,eventTimeInHou ...
- java实现excel模板导出
一. 准备工作 1. 点击此下载相关开发工具 2. 将poi-3.8.jxls-core-1.0两个jar包放到工程中,并引用 3. 将excel模板runRecord.xls放到RunRecordB ...
- 如何创建一个GitLab Web Hooks?
Git Hooks Git 能在特定的重要动作发生时触发自定义的脚本. 这些脚本都被存储在 Git 目录下的 hooks 子目录中(.git/hooks).当 git init 初始化一个仓库时,Gi ...
- ubuntu下安装lrzsz
secureCRT中可以使用rz和sz命令上传和下载文件,可是这要linux中安装了lrzsz才可以.我用的时候无法使用apt-get自动安装,下面介绍手动安装的方法. 1 下载lrzsz软件 ht ...