Codeforces Round #594 (Div. 2)

A - Integer Points

#include 
using namespace std;
typedef long long ll;
const int MAXN = 1e5+20;
int n,m;
int main() {
  int _;cin >> _;
  while(_--){
    cin >> n;
    ll odd=0,even=0;
    for (int i = 0; i < n; ++i) {
      int cur;
      cin >> cur;
      if(cur&1)odd++;
      else even++;
    }
    ll odd2=0,even2=0;
    cin >> n;
    for (int i = 0; i < n; ++i) {
      int cur;
      cin >> cur;
      if(cur&1)odd2++;
      else even2++;
    }
    cout << odd2*odd + even*even2 << endl;
  }
 
 
}

B - Grow The Tree

#include 
using namespace std;
typedef long long ll;
const int MAXN = 1e5+20;
int n;
ll arr[MAXN];
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  sort(arr,arr+n);
  ll l0=0,l1=0;
  for (int i = 0,j = n - 1,o=0; i <= j;o++) {
    if(o&1){
      l1+=arr[i++];
    }
    else l0+=arr[j--];
  }
  cout << l0*l0+l1*l1 << endl;
}

C - Ivan the Fool and the Probability Theory

#include 
using namespace std;
typedef long long ll;
const int MAXN = 1e5+20;
const int MOD = 1e9+7;
int n,m;
ll dp[MAXN][2];
int main() {
  cin >> n >> m;
  if(n < m)swap(n,m);
  dp[0][0]=1;
  for (int i = 1; i < n; ++i) {
    dp[i][0]=(dp[i-1][0]+dp[i-1][1])%MOD;
    dp[i][1]=dp[i-1][0];
  }
  ll ans0=(dp[n-1][0]+dp[n-1][1]-1+MOD)*2;
  ll ans1=(dp[m-1][0]+dp[m-1][1]-1+MOD)*2;
  cout << (ans0+ans1+2)%MOD << endl;
}

D1 - The World Is Just a Programming Task (Easy Version)

#include 
using namespace std;
typedef long long ll;
const int MAXN = 500+20;
const int MOD = 1e9+7;
int n,num[MAXN*2];
string s;
int main() {
  cin >> n;
  cin >> s;
  for (int i = 0; i < n; ++i) {
    if(s[i]=='(')num[i]=num[i+n]=1;
    else num[i]=num[i+n]=-1;
  }
  int ans=0,li=0,ri=0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      swap(num[i],num[j]);
      swap(num[i+n],num[j+n]);
 
      int start=0,maxn=0,sum=0,k=0;
      for (k = 0; k < n*2 && k - start < n && start < n; ++k) {
        sum += num[k];
        //if(i==2 && j==3)cout << " start " << start << endl;
        if(sum < 0){
          start = k+1,maxn=0,sum=0;
        }
        else if(sum==0){
          maxn++;
        }
      }
      if(sum!=0 || k - start != n){maxn=0;}
      if(ans < maxn){
        li=i,ri=j;
        ans=maxn;
      }
      ans=max(ans,maxn);
      swap(num[i],num[j]);
      swap(num[i+n],num[j+n]);
    }
  }
  cout << ans << endl;
  cout << li+1 << " " << ri+1 << endl;
}

D2 - The World Is Just a Programming Task (Hard Version)

#include 
using namespace std;
typedef long long ll;
const int MAXN = 3e5+20;
const int MOD = 1e9+7;
int n,num[MAXN*2],one[MAXN*2],two[MAXN*2];
vector<int>cycle;
string s;
int main() {
  while (cin >> n){
    cin >> s;
    cycle.clear();
    memset(one,0, sizeof(one));
    int even=0,odd=0;
    for (int i = 0; i < n; ++i) {
      if(s[i]=='('){
        num[i]=num[i+n]=1;
        even++;
      }
      else {
        num[i]=num[i+n]=-1;
        odd++;
      }
    }
    if(even != odd){
      puts("0");
      puts("1 1");
      return 0;
    }
 
    int start=0,maxn=0,sum=0;
    cycle.push_back(start);
    for (int k = 0; k < n*2 && k - start < n && start < n; ++k) {
      sum += num[k];
      //if(i==2 && j==3)cout << " start " << start << endl;
      if(sum < 0){
        start = k+1,maxn=0,sum=0;
        cycle.clear();
        cycle.push_back(start);
      }
      else if(sum==0){
        maxn++;
        cycle.push_back(k+1);
      }
      else if(sum==1){
        one[cycle.back()]++;
      }
//      else if(sum==2){
//        two[cycle.back()]++;
//      }
    }
    //cout << "cycle.size():" << cycle.size() << endl;
    //cout << "maxn:" << maxn << endl;
//  for(auto i : cycle){
//    cout << i << " " ;
//  }
    //cout << endl;
    int ans=maxn,li=0,ri=0;
    for (int i = 0; i < maxn; ++i) {
      if(one[cycle[i]] > ans){
        li = cycle[i],ri=cycle[i+1]-1;
        ans=one[cycle[i]];
      }
      int three=0,sum1=0,cl=cycle[i]+1;
      //cout<
      for (int k = cycle[i]+1; k < cycle[i+1]-1; ++k) {
        sum1+=num[k];
        if(sum1==0){
 
          if(ans < maxn+three){
            li=cl,ri=k;
            ans = maxn+three;
          }
          cl=k+1,three=0;
        }
        else if(sum1==1){
          three++;
        }
      }
    }
    cout << ans << endl;
    cout << (li%n)+1 << " " << (ri%n)+1 << endl;
    //return 0;
  }
}

E Queue in the Train

#include 
using namespace std;
typedef long long ll;
const int MAXN = 1e5+20;
const int MOD = 1e9+7;
map<ll,vector<int>>mt;
int n;
ll ans[MAXN];
set<int>q,wait;
queue<int>que;
ll mtime,mp,begint[MAXN];
void inqueue(){
  if(q.empty() && wait.empty())return;
  if(!q.empty() && wait.empty())return;
  if(q.empty() && !wait.empty()){
    int wminidx = (*(wait.begin()));
    wait.erase(wminidx);
    q.insert(wminidx);
    que.push(wminidx);
    begint[wminidx]=mtime;
    //cout << "begint[wminidx]: " << begint[wminidx] << endl;
    mt[mtime + mp].push_back(-1);
    return;
  }
  if(!q.empty() && !wait.empty()){
    int qminidx = (*(q.begin()));
    int wminidx = (*(wait.begin()));
    //cout << qminidx << " q w " << wminidx << endl;
    if (wminidx < qminidx) {
      wait.erase(wminidx);
      q.insert(wminidx);
      que.push(wminidx);
      begint[wminidx]=begint[que.front()] + mp*(q.size()-1);
      //cout << "begint[wminidx]: " << begint[wminidx] << endl;
      mt[begint[wminidx] + mp].push_back(-1);
      //cout << "winmidx:" << wminidx << endl;
    }
  }
}
int main() {
  cin >> n >> mp;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    mt[t].push_back(i);
  }
  auto it=(mt.lower_bound(0));
  mtime = it->first;
  for(int i = 0; i < it->second.size();i++)wait.insert(it->second[i]);
  inqueue();
 
  for (;;) {
    auto it = mt.upper_bound(mtime);
    if (it == mt.end())break;
    mtime = it->first;
    //cout << "mtime:" << mtime << endl;
    for (int i = 0; i < it->second.size(); ++i) {
      int v = it->second[i];
      //cout << "v:" << v << endl;
      if (v == -1) {
        int idx = que.front();
        //cout << "idx:" << idx << endl;
        que.pop();
        q.erase(idx);
        ans[idx] = mtime;
      } else {
        wait.insert(v);
      }
      inqueue();
    }
 
  }
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << " ";
  }
  puts("");
}

F Catowice City

#include 
using namespace std;
typedef long long ll;
const int MAXN = 1e6+20;
const int MAXM = 1e6+20;
const int MOD = 1e9+7;
vector<int>vec[MAXN];
vector<int>jury,contestant;
int st[MAXN],dfn[MAXN],id[MAXN];
int n,m,scc,top,cnt;
int dfs(int u)
{
    int lowu = dfn[u] = ++cnt;
    st[++top] = u;
    for(int i = 0;i < vec[u].size();i++)
    {
        int v = vec[u][i];
        if(!dfn[v])
        {
            int lowv = dfs(v);
            lowu = min(lowu,lowv);
        }
        else if(id[v] == 0)
            lowu = min(dfn[v],lowu);
    }
    if(dfn[u] == lowu)
    {
        scc++;
        do
        {
          if(scc==1)jury.push_back(st[top]);
          else contestant.push_back(st[top]);
          id[st[top--]] = scc;
        }while(st[top + 1] != u);
    }
 
    return lowu;
}
void solve(){
  memset(dfn,0,sizeof(int)*(n+2));
  memset(id,0,sizeof(int)*(n+2));
  cnt = top = scc = 0;
  jury.clear(),contestant.clear();
  for(int i = 1;i <= n;i++)if(!dfn[i])dfs(i);
  if(scc==1){
    puts("No");
  }
  else{
    puts("Yes");
    cout << jury.size() << " " << contestant.size() << endl;
    for (auto const& v:jury) cout << v << " ";
    puts("");
    for (auto const& v:contestant) cout << v << " ";
    puts("");
  }
}
int main() {
  int _;
  cin >> _;
  while(_--){
    scanf("%d%d",&n,&m);
    while(m--)
    {
      int u,v;
      scanf("%d%d",&u,&v);
      if(u != v)vec[u].push_back(v);
    }
    solve();
    for(int i = 1;i <= n;i++)vec[i].clear();
  }
 
}

你可能感兴趣的:(codeforce)