SDUT 2021 Spring Individual Contest - L (Gym - 101873)题解

48 篇文章 0 订阅
34 篇文章 0 订阅

题面链接

传送门

D - Pants On Fire

题目链接

答案

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

int n,m;
int tot;
map<string,int>mp;
vector<int>vp[222];
bool vis[222];
bool dp[222][222];

void dfs(int u,int v){
    vis[u]=1;
    dp[v][u]=1;
    for(auto i:vp[u]){
        if(!vis[i]) dfs(i,v);
    }
}

void solve(){
    cin>>n>>m;
    while(n--){
        string s,s1,s2;
        cin>>s1>>s>>s>>s>>s2;
        if(mp.find(s2)==mp.end()) mp[s2]=++tot;
        if(mp.find(s1)==mp.end()) mp[s1]=++tot;
        int u=mp[s2];
        int v=mp[s1];
        vp[u].pb(v);
    }
    for(int i=1;i<=tot;i++){
        mem(vis,0);
        dfs(i,i);
    }
    while(m--){
        string s,s1,s2;
        cin>>s1>>s>>s>>s>>s2;
        if(mp.find(s2)==mp.end()||mp.find(s1)==mp.end()){
            cout<<"Pants on Fire"<<endl;
            continue;
        }
        int u=mp[s2];
        int v=mp[s1];
        if(dp[u][v]) cout<<"Fact"<<endl;
        else{
            if(dp[v][u]) cout<<"Alternative Fact"<<endl;
            else cout<<"Pants on Fire"<<endl;
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

G - Water Testing

题目链接

答案

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

vector<PLL>edge;

void solve(){
    ll n;
    cin>>n;
    for(ll i=1;i<=n;i++){
        ll x,y;
        cin>>x>>y;
        edge.eb(make_pair(x,y));
    }
    ll point=0;
    ll area=0;
    PLL p=edge[n-1];
    for(int i=0;i<n;i++){
        area+=(edge[i].x+p.x)*(p.y-edge[i].y);
        if(edge[i].x==p.x) point+=abs(p.y-edge[i].y);
        else if(edge[i].y==p.y) point+=abs(p.x-edge[i].x);
        else point+=__gcd(abs(p.x-edge[i].x),abs(p.y-edge[i].y));
        p=edge[i];
    }
    area=abs(area/2);
    ll res=area-point/2+1;
    cout<<res<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

I - Uberwatch

题目链接

答案

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 3e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

int n,m;
int a[N];
int dp[N][12];

int dfs(int pos,int x){
    if(pos>=n) return 0;
    if(dp[pos][x]!=-1) return dp[pos][x];
    int k=0;
    if(x==m){
        k=dfs(pos+1,x);
        k=max(k,a[pos]+dfs(pos+1,1));
    }
    else k=dfs(pos+1,x+1);
    return dp[pos][x]=k;
}

void solve(){
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>a[i];
    mem(dp,-1);
    cout<<dfs(0,0)<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

K - You Are Fired

题目链接

答案

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

struct node{
    string s;
    ll d;
}dp[N];

bool cmp(node x, node y) {return x.d<y.d;}

void solve(){
    ll n,d,k;
    cin>>n>>d>>k;
    string s;
    ll dd;
    for(ll i=1;i<=n;i++){
        cin>>s>>dd;
        dp[i]={s,dd};
    }
    sort(dp+1,dp+1+n,cmp);
    ll sum=0;
    ll tot=0;
    for(ll i=n;i>=1;i--){
        if(sum>=d) break;
        if(tot>k) break;
        sum+=dp[i].d;
        tot++;
    }
    if(tot>k||sum<d) cout<<"impossible"<<endl;
    else {
        cout<<tot<<endl;
        for(ll i=n;i>n-tot;i--){
            cout<<dp[i].s<<", YOU ARE FIRED!\n";
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

B - Buildings (补)

题目链接

答案

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

ll power_mod(ll a,ll b){
    if(!b) return 1;
    if(b==1) return a%MOD;
    ll res=power_mod(a,b/2);
    res=(res*res)%MOD;
    if(b&1) res=(res*a)%MOD;
    return res%MOD;
}

void solve(){
    ll n,m,c;
    cin>>n>>m>>c;
    ll one=power_mod(c,n*n);
    ll res=0;
    ll inv=power_mod(m,MOD-2);
    for(ll i=1;i<=m;i++){
        ll g=__gcd(i,m);
        ll cur=power_mod(one,g);
        res+=cur;
        res%=MOD;
    }
    res*=inv;
    res%=MOD;
    cout<<res<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

C - Joyride(补)

题目链接

答案

法一:

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 1111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

int n,m,t,x;
int tot;
int h[M],cost[N],w[N];
ll dis[M][M];
bool vis[M][M];

struct edge{
    int u,v,w;
    int to,cost;
}ep[N];

struct node{
    int u,to;
    ll dist;
    bool operator<(const node &a) const{
        return dist>a.dist;
    }
};

inline void add_edge(int u,int v,int w){
    ep[++tot].u=u;
    ep[tot].v=v;
    ep[tot].w=w;
    ep[tot].to=h[u];
    h[u]=tot;
}

void Dijkstra(){
    dis[1][cost[1]]=w[1];
    priority_queue<node>q;
    q.push({1,cost[1],w[1]});
    while(q.size()){
        auto cur=q.top();
        q.pop();
        int u=cur.u;
        ll dist=cur.dist;
        int to=cur.to;
        if(vis[u][to]) continue;
        vis[u][to]=1;
        if(to+cost[u]<=x){
            if(dis[u][to+cost[u]]>dis[u][to]+w[u]){
                dis[u][to+cost[u]]=dis[u][to]+w[u];
                q.push({u,to+cost[u],dis[u][to+cost[u]]});
            }
        }
        for(int i=h[u];i;i=ep[i].to){
            int v=ep[i].v;
            int temp=to+ep[i].w+cost[v];
            if(temp>x) continue;
            if(dis[v][temp]>dis[u][to]+w[v]){
                dis[v][temp]=dis[u][to]+w[v];
                q.push({v,temp,dis[v][temp]});
            }
        }
    }
}

void solve(){
    cin>>x;
    cin>>n>>m>>t;
    while(m--){
        int u,v;
        cin>>u>>v;
        add_edge(u,v,t);
        add_edge(v,u,t);
    }
    for(int i=1;i<=n;i++) cin>>cost[i]>>w[i];
    for(int i=0;i<=n;i++){
        for(int j=0;j<=x;j++){
            dis[i][j]=INF;
            vis[i][j]=0;
        }
    }
    Dijkstra();
    if(dis[1][x]==INF) cout<<"It is a trap."<<endl;
    else cout<<dis[1][x]<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

法二:

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 1111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

ll x,n,m,t;
ll cost[N],w[N];
vector<ll>G[N];
ll dp[M][M];

ll f(ll u,ll ti){
    if(ti+cost[u]>x) return INF;
    if(ti+cost[u]==x){
        if(u==1) return w[u];
        return INF;
    }
    ll &res=dp[u][ti];
    if(res!=-1) return res;
    res=INF;
    ll a=cost[u];
    ll b=w[u];
    res=min(res,f(u,ti+a)+b);
    for(auto v:G[u]) res=min(res,f(v,ti+t+a)+b);
    return res;
}

void solve(){
    cin>>x;
    cin>>n>>m>>t;
    while(m--){
        ll u,v;
        cin>>u>>v;
        G[u].pb(v);
        G[v].pb(u);
    }
    for(ll i=1;i<=n;i++) cin>>cost[i]>>w[i];
    mem(dp,-1);
    ll res=f(1,0);
    if(res>=INF) cout<<"It is a trap."<<endl;
    else cout<<res<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值