Codeforces Round #547 (Div. 3)

https://codeforces.com/contest/1141/problem/B

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int a[maxn],b[maxn];
int main()
{
    int n;
    while ( cin >> n )
    {
        for(int i=0;i<n;i++)
            cin>>a[i];


        int ans=0;
        int maxs=0;
        for(int i=0;i<2*n;i++)
        {
            if(a[i%n]==1)
            {
                ans++;
            }
            else
            {

                ans=0;
            }
            maxs=max(ans,maxs);
        }
        cout<<min(n,maxs)<<endl;
    }
}

https://codeforces.com/contest/1141/problem/C

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int a[maxn];
int main()
{
    int n;
    while ( cin >> n )
    {
        int x, ans = 0,y=0,z=0;
        for ( int i = 2; i <= n; i++ )
        {
            cin >> a[i];
            ans+=a[i];
            if(ans<1)
            {
                z+=(1-ans);
                ans=1;
            }
        }
        cout<<z<<' ';
        for(int i=2;i<=n;i++)
        {
            z+=a[i];
            cout<<z<<' ';
        }
        cout<<endl;

    }
}

https://codeforces.com/contest/1141/problem/D

#include <bits/stdc++.h>
using namespace std;
int n;
map<char, stack<int > >a,b;
stack< pair<int, int > > tag;
int main() {

    while ( cin >> n ) {
        string s1, s2;
        cin >> s1 >> s2;
        for ( int i = 0; s1[i]; i++ ) {
            a[s1[i]].push ( i + 1 );
        }
        for ( int i = 0; s2[i]; i++ )
        {
            if(s2[i]=='?')
            {
              b['?'].push(i+1);
              continue ;
            }
            else if ( a[s2[i]].size() ) {
                tag.push ( make_pair ( a[s2[i]].top(), i + 1  ) );
                a[s2[i]].pop();
            } else if ( a['?'].size() ) {
                tag.push ( make_pair ( a['?'].top(), i + 1  ) );
                a['?'].pop();
            }
        }
        for(int i=0;s1[i];i++)
        {
          if(a[s1[i]].size() &&b['?'].size())
          {
            tag.push ( make_pair ( a[s1[i]].top(), b['?'].top()  ) );
            a[s1[i]].pop();
            b['?'].pop();
          }
        }
        cout << tag.size() << endl;
        while ( !tag.empty() ) {
            cout << tag.top().first << ' ' << tag.top().second << endl;
            tag.pop();
        }
    }
}

https://codeforces.com/contest/1141/problem/E

数学公式推理:
if(tag[n]==0) tag[n]=-1;
timee=(max((long long )0,h-tag[i])/tag[n]+( (h-tag[i])%tag[n]>0 &&tag[n]>0) )*n+i;
遍历一遍寻找一个最小时间


#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5+5;
long long a[maxn], tag[maxn];
int main()
{
    long long h,n;
    while(cin>>h>>n)
    {
        long long q=0,times=1e18;
        for(long long i=1;i<=n;i++)
        {
            cin>>a[i];
            a[i]=-a[i];
            tag[i]+=tag[i-1]+a[i];
        }
        if(tag[n]==0) tag[n]=-1;
        for(int i=1;i<=n;i++)
        {
           long long  timee=(max((long long )0,h-tag[i])/tag[n]+( (h-tag[i])%tag[n]>0 &&tag[n]>0) )*n+i;
           if(timee>0) times=min(times,timee);
        }
        if(times<1e18)cout<<times<<endl;
        else cout<<"-1"<<endl;
    }
}

https://codeforces.com/contest/1141/problem/F2

题解 贪心看每个和的右边,取一个最小的,然后和下次的这个和对比,如果下次的这个和的左边存在大于上次右边的,
则这个和的记录++

#include <bits/stdc++.h>
using namespace std;
std::map<int, int > tag_r;
std::map<int, vector<pair<int, int > >  > dp;
int sum[10005];
int main() {
    int n;
    while(cin >> n) {
        int x, mx = 0, vs;
        for(int i = 1; i <= n; i++) {
            cin >> x;
            sum[i] = sum[i - 1] + x;
            for(int j = 1; j <= i; j++) {
                int ans = sum[i] - sum[j - 1];
                if(j > tag_r[ans]) {
                    tag_r[ans] = i;
                    dp[ans].push_back( make_pair(j, i) );
                    if(dp[ans].size() > mx) {
                        mx = dp[ans].size();
                        vs = ans;
                    }
                }
            }
        }
        cout << mx << endl;
        for(int i = 0; i < mx; i++) {
            cout << dp[vs][i].first << ' ' << dp[vs][i].second << endl;
        }
    }
}

https://codeforces.com/contest/1141/problem/G

题解:
轻松的找到。最少使用多少种颜色。
就是 找到第m+1 小的点。
然后 对每个边循环染色。
这样 小于等于m+1 的边的点,绝对不会重复

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
int n,m;
#define mp make_pair
vector< pair <int ,int > > grp[maxn];
struct node
{
    int x;
    int nums=0;
} d[maxn] ;
bool cmp(node &a,node &b)
{
    return a.nums>b.nums;
}
int tag[maxn] ;
int maxr;
void dfs(int node ,int dad,int r)
{
    int le=0;
    for(int i=0;i<grp[node].size();i++)
    {
        int son=grp[node][i].first;
        int vis=grp[node][i].second;
        if(son!=dad)
        {
           // cout<<node<<' '<<son<<' '<<vis<<' '<<(r+i+1)<<' '<<r<<endl;
            tag[vis]=(r+le+1)%maxr;
            dfs(son,node, (r+le+1)%maxr);
            le++;
        }
    }
}
int main()
{
    while(cin>>n>>m)
    {
        int x,y;
        for(int i=1;i<n;i++)
        {
            cin>>x>>y;
            d[x].nums++;
            d[y].nums++;
            grp[x].push_back( mp (y,i ));
            grp[y].push_back( mp (x,i) );
        }
        sort(d+1,d+1+n,cmp) ;
        maxr=d[m+1].nums;
        dfs(1,0,0);
        cout<<maxr<<endl;
        for(int i=1;i<n;i++)
        {
            cout<<tag[i]+1<<' ';
        }
        cout<<endl;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值