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)的更多相关文章

  1. 拓扑序+dp Codeforces Round #374 (Div. 2) C

    http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...

  2. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  3. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  4. Codeforces Round #374 (Div. 2) B. Passwords 贪心

    B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...

  5. Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crosswor 水题

    A. One-dimensional Japanese Crossword 题目连接: http://codeforces.com/contest/721/problem/A Description ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #374 (Div. 2) B. Passwords —— 基础题

    题目链接:http://codeforces.com/contest/721/problem/B B. Passwords time limit per test 2 seconds memory l ...

  9. 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 ...

随机推荐

  1. HTML5-03 页面布局

    概述 HTML 文档中的元素是一个接着一个排列的,只是简单地在在块级元素的前后加上拆行,是一种流水布局.但是,我们所见到的 Web 页面按照一定的规则布局排版的(通常是多列的),所以就要借助一定的方法 ...

  2. ArrayList 浅析示例

    package com.smbea.demo; import java.util.ArrayList; import java.util.Iterator; import java.util.List ...

  3. LoadRunner 11 安装步骤

    loadrunner 安装步骤: LoadRunner11下载:  在网上可以搜索到,在这个就不提供了. LoadRunner11原理: 破解方法和以前版本相同,我用的是LR8.0的破解文件,同样实用 ...

  4. RAC异机恢复

    RAC异机恢复PDCL到PFCL: PNCL:RAC+ASM ,product env   db name:PNCL   instance:PDCL1 PDCL2 PFCL:RAC+ASM ,perf ...

  5. 足球游戏论坛数据分析--简单粗暴的K均值聚类

    在<<足球游戏论坛数据分析--简单粗暴的贝叶斯>>中尝试了贴标签后,一直觉得结果无法接受, 慢慢回想, 其实选择的算法是错误的,原因有 论坛帖子分类并非就是PC/PS/XBOX ...

  6. Android入门笔记(一)

    第一部分,Android开发环境的搭建 1.去http://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新版本jdk并安 ...

  7. sql中毫秒数与格式化时间的转换

    使用MYSQL自带的函数FROM_UNIXTIME(unix_timestamp,format). 如: SELECT FROM_UNIXTIME(1461201575895/1000,"% ...

  8. WPFTookit Chart 入门

    如何使用WPFToolKit Chart private void button1_Click(object sender, EventArgs e) { var s = new Series(); ...

  9. Windows7台式电脑怎么调节屏幕亮度

     

  10. Java程序设计之裴波拉切那数列(兔子一年的数量)

    题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5 ...