JC5. Midterm exam

Game address: https: //www.luogu.com.cn/contest/26179

T1: direct simulation.

T2: direct simulation.

T3: calculated considering the DP; enumeration position

T4:

Practice 1: Violence

Practice 2: k = 1, only one man traveled the entire tree

Practice 3: DFS

Practices 4: binary answer

 

std (Official Version)

T1:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long LL;

int main() {
    LL n, t;
    scanf("%lld%lld", &n, &t);
    printf("%lld %lld\n", max(t-6*n, 0LL), (t-n)/6);
}

T2:

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cmath>
#define ll long long
#define INF 0x3fffffff
#define clr(x) memset(x,0,sizeof(x))

using namespace std;

inline int read()
{
    register int ret=0,c=getchar();
    while(!isdigit(c))c=getchar();
    while(isdigit(c))ret=ret*10+c-'0',c=getchar();
    return ret;
}

int S,I,R,n;
double b,g;

int main()
{
    S=read(),I=read(),n=read();
    scanf("%lf%lf",&b,&g);
    for(int i=1;i<=n;i++)
    {
        ll di=ceil(b*S*I),dr=ceil(g*I);
        if(di>S)di=S; 
        S-=di;
        I+=di-dr;
        R+=dr;
    }
    printf("%d %d %d\n",S,I,R);
    return 0;
}

T3:

#include<bits/stdc++.h>
#define lson (o<<1)
#define rson (o<<1|1)
#define fi first
#define sc second
#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
using namespace std;
const double pi=acos(-1);
const double eps=1e-6;
inline int lowbit(int x){return x&(-x);}
inline int read(){
    int f=1,x=0;char ch;
    do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
    do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
    return f*x;
}
template<typename T> inline T max(T x,T y,T z){return max(max(x,y),z);}
template<typename T> inline T min(T x,T y,T z){return min(min(x,y),z);}
template<typename T> inline T sqr(T x){return x*x;}
template<typename T> inline void checkmax(T &x,T y){x=max(x,y);}
template<typename T> inline void checkmin(T &x,T y){x=min(x,y);}
template<typename T> inline void read(T &x){
x=0;T f=1;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}
template<typename A,typename B,typename C> inline A fpow(A x,B p,C YQL) { 
    A years=1;
    for(;p;p>>=1,x=1LL*x*x%yql)if(p&1)ans=1LL*x*ans%yql;
    return ans;
}
struct FastIO{
    static const int S=1310720;
    int wpos;char wbuf[S];
    FastIO():wpos(0) {}
    inline int xchar(){
        static char buf[S];
        static int len=0,pos=0;
        if(pos==len)pos=0,len=fread(buf,1,S,stdin);
        if(pos==len)return -1;
        return buf[pos++];
    }
    inline int read(){
        int c=xchar(),x=0;
        while(c<=32&&~c)c=xchar();
        if(c==-1)return -1;
        for(;'0'<=c&&c<='9';c=xchar())x=x*10+c-'0';
        return x;
    }
}io;
//#define read io.read

const int N=1e5+10;
const int yql=1e9+7;
int n,m,dp[N];
char s[N];
inline ll stn(int l,int len){
    ll x=0;
    for(int i=0;i<len;i++)x=x*10+(s[i+l]-'0');
    return x;
}
inline void solve(){
    memset(dp,0,sizeof(dp));
    memset(s,0,sizeof(s));
    ll k;
    n=read();read(k);
    scanf("%s",s);
    dp[n]=1;
    for(int i=n-1;~i;i--)
        for(int j=1;i+j<=n;j++){
            ll t=stn(i,j);
            if(t>=k)break;
            dp[i]=(dp[i]+dp[i+j])%yql;
        }
    printf("%d\n",dp[0]);
}
int main(){
    solve();
}

T4:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long LL;

const int MAXN = 200010;

struct Edge {
    int from, to;
    LL dist;
    Edge () {}
    Edge (int _from, int _to, LL _dist) : from(_from), to(_to), dist(_dist) {}

    bool operator<(const Edge &rhs) const {
        return to < rhs.to;
    }
};

vector<Edge> g[MAXN];

int n, k;
LL l, r;

inline void insert(int u, int v, LL w) {
    g[u].push_back(Edge(u, v, w));
    g[v].push_back(Edge(v, u, w));
}

Edge a[MAXN];
LL d[MAXN];
bool lf[MAXN];

int dfs_clock;
inline void dfs(int now, int fa) {
    // for (auto e : g[now]) {
    // for (vector::iterator it = g[now].begin(); it != g[now].end(); it++) {
    //     Edge &e = *it;
    for (int i = 0; i < g[now].size(); i++) {
        Edge &e = g[now][i];

        if (e.to == fa) continue;
        a[dfs_clock++] = e;
        d[e.to] = d[now] + e.dist;
        l = max(d[e.to], l);
        dfs(e.to, now);
    }
    if (g[now].size() == 1 && now != 1) lf[now] = true;
}

inline bool test(LL x) {
    int lst = 0;
    for (int i = 0; i < k; i++) {
        LL totd = 0;
        for (; !lf[a[lst].to]; lst++) ;
        totd += d[a[lst++].to];
        for (; lst < n-1; lst++) {
            if (totd + a[lst].dist <= x) totd += a[lst].dist;
            else break;
        }
        if (lst == n-1) return true;
    }
    return false;
}

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        LL w;
        scanf("%d%d%lld", &u, &v, &w);
        insert(u, v, w);
        r += w;
    }
    for (int i = 1; i <= n; i++) {
        sort(g[i].begin(), g[i].end());
    }
    dfs(1, 0);
    while (l < r) {
        LL mid = (l + r) / 2;
        if (test(mid)) r = mid;
        else l = mid + 1;
    }
    printf("%lld\n", l*2);
}

 

Guess you like

Origin www.cnblogs.com/2019computer/p/12324405.html