首页 > 代码库 > Codeforces Round #374 (div.2)遗憾题合集

Codeforces Round #374 (div.2)遗憾题合集

C.Journey

读错题目了。。。不是无向图,结果建错图了(喵第4样例是变成无向就会有环的那种图)

并且这题因为要求路径点尽可能多

其实可以规约为限定路径长的拓扑排序,不一定要用最短路做

技术分享
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<iostream>using namespace std;typedef long long LL;const double pi=acos(-1.0),eps=1e-6;void File(){    freopen("D:\\in.txt","r",stdin);    freopen("D:\\out.txt","w",stdout);}template <class T>inline void read(T &x){    char c = getchar();    x = 0;    while(!isdigit(c)) c = getchar();    while(isdigit(c)) { x = x * 10 + c - 0; c = getchar(); }}const int INF=0x7fffffff;const int maxn=5010;int dp[maxn][maxn];struct Edge{    int v,nx; int w;}e[maxn];int h[maxn],sz,r[maxn];int n,m,T;queue<int>Q;int pre[maxn][maxn];void add(int u,int v,LL w){    e[sz].v=v; e[sz].w=w;    e[sz].nx=h[u]; h[u]=sz++;}int main(){    scanf("%d%d%d",&n,&m,&T);    for(int i=0;i<=n;i++)    {        h[i]=-1;        for(int j=0;j<=n;j++)        {            dp[i][j]=INF;            pre[i][j]=-1;        }    }    for(int i=1;i<=m;i++)    {        int u,v; LL w; scanf("%d%d%d",&u,&v,&w);        add(u,v,w); r[v]++;    }    dp[1][1]=0;    for(int i=1;i<=n;i++) if(r[i]==0) Q.push(i);    bool flag=0;    while(!Q.empty())    {        int top=Q.front(); Q.pop();        if(top==1) flag=1;        if(flag==0)        {            for(int i=h[top];i!=-1;i=e[i].nx)            {                int to=e[i].v;                r[to]--;                if(r[to]==0) Q.push(to);            }            continue;        }        for(int i=h[top];i!=-1;i=e[i].nx)        {            int to=e[i].v;            for(int j=1;j<=n;j++)            {                if(dp[top][j-1]==INF) continue;                if(dp[top][j-1]+e[i].w>T) continue;                if(dp[top][j-1]+e[i].w>=dp[to][j]) continue;                pre[to][j]=(top-1)*n+j-1-1;                dp[to][j]=dp[top][j-1]+e[i].w;            }            r[to]--;            if(r[to]==0) Q.push(to);        }    }    int sum;    for(int i=1;i<=n;i++) if(dp[n][i]<=T) sum=i;    cout<<sum<<endl;    int nowx=n,nowy=sum; stack<int>S;    while(1)    {        S.push(nowx);        int tx,ty;        tx=pre[nowx][nowy]/n; tx++;        ty=pre[nowx][nowy]%n; ty++;        if(pre[nowx][nowy]==-1) break;        nowx=tx; nowy=ty;    }    while(!S.empty())    {        cout<<S.top()<<" "; S.pop();    }    return 0;}
View Code

D.Maxim and Array

没时间做。。。被第2、3题耽误了

试算了一下发现并不复杂。。。每次取绝对值最小的数(使其余值的乘积绝对值最大)这样对其加减时,总乘积变化也就最大

太多数了,直接乘会爆long long,直接判断负数个数(总乘积的正负性),以此判断要加要减

技术分享
#include <stdio.h>#include <algorithm>#include <string.h>#include <iostream>#include <queue>#include <math.h>using namespace std;const int N = 200000 + 50;typedef long long ll;ll aabs(ll x) {return x<0 ? -x : x;}struct node{    ll num;    int id;    bool operator < (const node & temp) const    {        return aabs(num) > aabs(temp.num);    }};ll a[N];ll n,k,x;int main(){    cin >> n >> k >> x;    int sign = 1;    priority_queue<node> Q;    for(int i=1;i<=n;i++)    {        scanf("%I64d",a+i);        if(a[i] < 0) sign = -sign;        Q.push((node){a[i],i});    }    while(k--)    {        node temp = Q.top();Q.pop();        if(a[temp.id] < 0)        {            if(sign == -1) a[temp.id] -= x;            else a[temp.id] += x;            if(a[temp.id] >= 0) sign = -sign;        }        else        {            if(sign == -1) a[temp.id] += x;            else a[temp.id] -= x;            if(a[temp.id] < 0) sign = -sign;        }        Q.push((node){a[temp.id],temp.id});    }    for(int i=1;i<=n;i++)    {        printf("%I64d%c",a[i],i==n?\n: );    }}
View Code

E.Road to Home

Codeforces Round #374 (div.2)遗憾题合集