Codeforce 2149(div3

A

水题

#include
#include
#include
#include
using namespace std;
int arr[110];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;cin>>q;
    while(q--)
    {
        int n;cin>>n;
        for(int i=1;i<=n;++i)
            cin>>arr[i];
        sort(arr+1,arr+1+n);
        int ans = 1;
        for(int i=1;i<n && ans==1;++i)
            if(arr[i]==arr[i+1]-1)
                ans=2;
        cout<<ans<<endl; 
    }
    return 0;
}

B

水题

#include
#include
#include
#include
#include
#include
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
template<class TT>
inline void read(TT& n)
{
    TT X=0,w=1;char ch = -1;
    while(ch<'0' ||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0' &&ch<='9'){X = (X<<3)+(X<<1)+ch-'0';ch=getchar();}
    n = X*w;
}
const int N = 2e5+10;
bool vis[N];
int to[N];
int ans[N];
int dfs(int now,int s,int times)
{
    vis[now]=1;
    if(to[now]==s)
        return ans[now] = times;
    return ans[now] = dfs(to[now],s,times+1);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;read(T);
    while(T--)
    {
    	 int n;read(n);
        for(int i=1;i<=n;++i)
        	vis[i]=0;
        for(int i=1;i<=n;++i)
            read(to[i]);
        for(int i=1;i<=n;++i)
        {
            if(vis[i]==0)
                dfs(i,i,1);
        }
        for(int i=1;i<=n;++i)
            printf("%d ",ans[i]);
        cout<<endl;
    } 
    return 0;
}

C

乱搞

#include
#include
#include
#include
#include
#include
#define rep(i,a,n) for(int i=(int)(a);i<(int)(n);++i)
#define repb(i,a,n) for(int i=(int)(a);i<=(int)(n);++i)
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
template<class TT>
inline void read(TT& n)
{
    TT X=0,w=1;char ch = -1;
    while(ch<'0' ||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0' &&ch<='9'){X = (X<<3)+(X<<1)+ch-'0';ch=getchar();}
    n = X*w;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;read(T);
    while(T--)
    {
        ll n;read(n);
        ll ret = 1,anx = 1;
        ll ans = 0,ans1=0;
        while(ret<n)
        	ret*=3;
       	ans = ret;
       	anx = ret/3;
        while(anx)
        {
        	if(ans1+anx<n)
        		ans1+=anx;
       		else
       			ans = min(ans,ans1+anx);
        	anx/=3;
        }
        cout<<ans<<endl;
    }    
    return 0;
}

D

贪心扫一遍,如果n范围大一点的话要离散化

#include
#include
#include
#include
#include
#include
using namespace std;
const int N = 2e5+10;
int cf[N];
int ans[N],cnt;
struct node
{
	int id,r;
    node(int id,int r):id(id),r(r){};
    bool operator<(const node &u)const
    {
        return r<u.r;
    }
};
priority_queue<node>q;
vector<node>vec[N];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int m,k;cin>>m>>k;
    for(int i=1;i<=m;++i)
    {
        int l,r;cin>>l>>r;
        vec[l].push_back(node(i,r));
    }
    int now=0;
    for(int i=1;i<N;++i)
    {
        now+=cf[i];
        for(int j=0;j<vec[i].size();++j)
        {
            now++;
            cf[vec[i][j].r+1]--;
            q.push(node(vec[i][j].id,vec[i][j].r));
        }
        while(now>k)
        {
            now--;
            node t = q.top();
            ///cout<
            q.pop();
            ans[cnt++] = t.id;
            cf[t.r+1]++;
        }
    }
    cout<<cnt<<endl;
    sort(ans,ans+cnt);
    for(int i=0;i<cnt;++i)
        cout<<ans[i]<<" ";
    return 0;
}

E

很裸的dp

#include
#include
#include
using namespace std;
const int N = 2e5+10;
int arr[N],brr[N];
int dp[N][2];
int main()
{
    memset(dp,0x3f,sizeof(dp));
    int n,m;cin>>n>>m;
    for(int i=1;i<n;++i)
        cin>>arr[i];
    for(int i=1;i<n;++i)
    {
        cin>>brr[i];
    }
    dp[0][0]=0;
    for(int i=1;i<=n;++i)
    {
	 	dp[i][0]=min(dp[i-1][0],dp[i-1][1])+arr[i-1];
		dp[i][1]=min(dp[i-1][1],dp[i-1][0]+m)+brr[i-1];  	
        cout<<min(dp[i][0],dp[i][1])<<" ";
    }
    return 0;
}

你可能感兴趣的:(codeforce)