CCF-CSP_201809(第14次)

1. 卖菜


#include<iostream>
using namespace std;

int a[100000];//a[0],a[n+1]=0;
int main(){
    int n;
    
    scanf("%d", &n);
    for(int i = 1;i <= n; ++i)
        scanf("%d", &a[i]);
    for(int i = 1;i <= n; ++i){
        int ans = a[i-1] + a[i] + a[i+1];
        (i == 1 ||i == n) ?  printf("%d ",ans / 2) : printf("%d ",ans / 3);
    }
    return 0;
}
8
4 1 3 1 6 5 17 9

2 2 1 3 4 9 10 13 

2. 买菜


//简单模拟,hash方法,暴力方法
#include<iostream>
using namespace std;
const int N=1000005;
 
int main()
{
    int n, x, y, flag[N]={0};
    long long ans = 0;
    
    scanf("%d", &n);
    for(int i = 1;i <= 2*n;i++){
        scanf("%d%d", &x, &y);
        for(int j = x;j < y;j++)
          flag[j]++;
    }
    
    for(int i = 1;i < N;i++){
      if(flag[i] == 2)
        ans++;
    }
    printf("%lld\n", ans);
    
    return 0;
}
4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14

3

3. 元素选择器


#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>

using namespace std;
const int MAXN = 105;

struct Selector{
    int row;
    int rank;
    string label;//html,head,title,body,h1,p,div
    string id;//#property
};
Selector html[MAXN];

int main(){
    int n,m;
    
    scanf("%d%d", &n, &m);
    getchar();
    for(int i = 1;i <= n; ++i){
        string ht, str;
        int num = 0;
        
        getline(cin, ht);
        istringstream ss(ht);
        ss >> str;//"."
        
        while(str[num] == '.') num++;
        html[i].row = i;
        html[i].rank = num / 2;
        html[i].label = str.substr(num);
        
        //Labels are not case sensitive
        transform(html[i].label.begin(), html[i].label.end(), html[i].label.begin(), ::tolower);
        
        //Get id property
        while(ss >> str){
            if(str[0] == '#')
                html[i].id = str;
        }
    }//for end
    
    for(int i = 0;i < m; ++i){
        string line, str;
        vector<string> query;
        int cnt = 0, num, flag[MAXN];
        
        getline(cin, line);
        istringstream ss(line);
        while(ss >> str){
            if(str[0] != '#'){//Labels are not case sensitive
                transform(str.begin(),str.end(),str.begin(), ::tolower);
            }
            query.push_back(str);//Store query keywords.For example,(div div p)
        }
        
        //Compare from back to front
        vector<string>::reverse_iterator it = query.rbegin();
        for(int j = 1;j <= n; ++j){
            if(html[j].id == *it || html[j].label == *it)//The last query keyword(div div p:p)
                flag[cnt++] = j;//Record all possible line id
        }
        num = cnt;//Record total number of possible lines
        //Up to (cnt) lines to meet the conditions, continue to compare
        for(int k = 0;k < cnt; ++k){//Judge one by one
            it = query.rbegin() + 1;//(div div p:div)
            int mrank = html[flag[k]].rank;
            
            for(int j = flag[k] - 1;it != query.rend() && j > 0; --j){
                if(html[j].rank < mrank && (*it == html[j].label || *it == html[j].id)){
                    ++it;
                    mrank = html[j].rank;
                }
            }
            if(it != query.rend()){
                flag[k] = 0;
                num--;
            }
        }
        
        cout << num;//total number of lines
        for(int j = 0;j < cnt; ++j){
            if(flag[j])  cout << " " << flag[j];
        }
        cout << endl;
        
    }//for end
    
    return 0;
}
11 5
html
..head
....title
..body
....h1
....p #subtitle
....div #main
......h2
......p #one
......div
........p #two
p
#subtitle
h3
div p
div div p

3 6 9 11
1 6
0
2 9 11
1 11

4. 再卖菜


//差分约束:最长路求解最小值(将边权变为相反值,求最短路,结果的相反数就是要求的最长路)
//c1<=dist[i]-dist[j]<=c2
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int N=310;
const int INF=0x3f3f3f3f;
 
struct Edge{
    int v,w;
    Edge(int v,int w):v(v),w(w){}
};
 
vector<Edge>g[N];
int dist[N];
bool vis[N];
int a[N];
 
void spfa(int s)
{
    memset(vis,false,sizeof(vis));
    fill(dist,dist+N,INF);
    queue<int>q;
    
    q.push(s);
    vis[s]=true;
    dist[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i].v;
            int w=g[u][i].w;
            if(dist[v]>dist[u]+w)
            {
                dist[v]=dist[u]+w;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
}
 
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
      scanf("%d",&a[i]);
    //2a1<=s[2]-s[0]<=2a1+1
    g[0].push_back(Edge(2,-2*a[1]));
    g[2].push_back(Edge(0,2*a[1]+1));
    for(int i=2;i<n;i++)
    {
        //3ai<=s[i+1]-s[i-2]<=3ai+2
        g[i-2].push_back(Edge(i+1,-3*a[i]));
        g[i+1].push_back(Edge(i-2,3*a[i]+2));
    }
    //2an<=s[n]-s[n-2]<=2an+1
    g[n-2].push_back(Edge(n,-2*a[n]));
    g[n].push_back(Edge(n-2,2*a[n]+1));
    //s[i]-s[i-1]>=1
    for(int i=1;i<=n;i++)
      g[i-1].push_back(Edge(i,-1));
    spfa(0);
    for(int i=1;i<=n;i++)
      printf("%d ",-dist[i]-(-dist[i-1]));
    printf("\n");
    return 0;
}

5.线性递推式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值