zoukankan      html  css  js  c++  java
  • CodeForces

    C题:暴力模拟

    贪心的思想,我以来就用的是while来做,在第38个样例那里wa了,
    然后自己想了想,应该是我第一次写的时候因为是在while里面更新index,没有每一次进行比较,后面改成for循环,然后就过了。
    注意:题目说了,因为女士优先,所以当两个人吃到相同的巧克力棒,还剩下最后一根的时候是让给女士的! #include
    <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <iomanip> #include <algorithm> #include <cctype> #include <stack> #include <queue> #include <string> #include <cstring> #include <iomanip> #include <set> #include <vector> #include <cstdio> #include <stack> #include <sstream> #include <cstring> #include <map> using namespace std; const int INF=0x3f3f3f3f; int arr[100005]; int main() { int lenth; cin>>lenth; for(int i=0;i<lenth;i++) cin>>arr[i]; int i=0,j=lenth-1; int suml=0,sumr=0; for(;i<=j;) { if(suml<=sumr) { suml+=arr[i]; i++; continue; } if(suml>sumr) { sumr+=arr[j]; j--; } } //cout<<suml<<" "<<sumr<<endl; cout<<i<<" "<<lenth-i<<endl; return 0; }

    D题:直接dfs暴搜一个个联通块,答案就是num-1。或者用并查集

    具体:用一个二维数组存储人和红娘直接的关系,然后直接暴搜

      1 ///dfs写法
      2 #include <bits/stdc++.h>
      3 #include<algorithm>
      4 using namespace std;
      5 int n,m;
      6 const int N=1e2+10;
      7 
      8 int ma[N][N];
      9 bool vis[N];
     10 
     11 
     12 void dfs(int x)
     13 {
     14     for(int i=1;i<=m;i++)
     15     {
     16         if(ma[i][x])
     17         {
     18             for(int j=1;j<=n;j++)
     19             {
     20                 if(!vis[j]&&ma[i][j])
     21                 {
     22                     vis[j]=1;
     23                     dfs(j);
     24                 }
     25             }
     26         }
     27     }
     28 }
     29 
     30 int main()
     31 {
     32     int x,y;
     33     bool flag=false;
     34     scanf("%d%d",&n,&m);
     35 
     36     memset(ma,0,sizeof(ma));
     37     for(int i=1;i<=n;i++)
     38     {
     39         scanf("%d",&x);
     40         if(x)
     41             flag=true;
     42         for(int j=1;j<=x;j++)
     43         {
     44             scanf("%d",&y);
     45             ma[y][i]=1;
     46         }
     47     }
     48     if(!flag)
     49     {
     50         printf("%d
    ",n);
     51         return 0;
     52     }
     53 
     54     int ans=0;
     55     memset(vis,0,sizeof(vis));
     56     for(int i=1;i<=n;i++)
     57     {
     58         if(!vis[i])
     59         {
     60             vis[i]=1;
     61             dfs(i);
     62             ans++;
     63         }
     64     }
     65     printf("%d
    ",ans-1);
     66     return 0;
     67 }
     68 ///并查集
     69 #include"iostream"
     70 #include"algorithm"
     71 #include"cstring"
     72 #include"set"
     73 using namespace std;
     74 int pre[150];
     75  
     76 void init()
     77 {
     78     for(int i = 0;i < 101;i ++)
     79     {
     80         pre[i]=i;
     81     }
     82 }
     83 int find(int x)
     84 {
     85     return x==pre[x] ? x:pre[x]=find(pre[x]);
     86 }
     87 void join(int x,int y)
     88 {
     89     int fx=find(x);
     90     int fy=find(y);
     91     if(fx != fy)
     92     {
     93         pre[fx]=fy;
     94     }
     95 }
     96  
     97 int main()
     98 {
     99     int n,m;
    100     while(cin >> n >> m)
    101     {
    102         init();
    103         int vst[105]={0};
    104         int sum =0 ;
    105         for(int i = 0;i < n;i ++)
    106         {
    107             int t;
    108             cin >> t;
    109             int a[105];
    110             if(t==0)
    111             {
    112                 sum ++;
    113                 continue;
    114             }
    115             cin >> a[0];
    116             vst[a[0]]++;
    117             for(int  j = 1;j < t;j ++)
    118             {
    119                  cin >>a[j];
    120                  vst[a[j]]++;
    121                  join(a[j-1],a[j]);
    122             }
    123              
    124         }
    125         int pl = 0;
    126     /*    cout<<"-------------"<<endl;
    127         for(int i = 1;i <= m;i ++)
    128         {
    129             cout<<pre[i]<<" "<<i<<endl;
    130         }
    131         cout<<"-------------"<<endl;*/
    132         for(int i = 1;i <= m;i ++)
    133         {
    134             if(vst[i])
    135             {
    136                 if(pre[i]==i)
    137                 {
    138                     pl++;
    139                 }
    140             }
    141         }
    142     //    cout<<pl<<endl;
    143         pl=max(0,pl-1);
    144         cout<<sum+pl<<endl;
    145         
    146     }
    147     return 0;
    148 } 
  • 相关阅读:
    Selenium IDE
    selenium简介
    Java程序要操作数据库,一定要使用JDBC技术吗?
    JDBC几个接口分区叫什么?它们分别有什么用?
    JDBC的全称是什么?
    什么时候使用session?什么时候使用application?
    内置对象session和application的作用是什么?
    修改表单中为什么要使用隐藏域?
    如何创建一个不能修改的属性?
    如何修改属性?
  • 原文地址:https://www.cnblogs.com/yrz001030/p/12246438.html
Copyright © 2011-2022 走看看