zoukankan      html  css  js  c++  java
  • 【NOIP2017】【Luogu3955】图书管理员(枚举,取模得后缀)

    problem

    • n个图书编号,q个需求码。(都是数字)
    • 求满足所有包含需求码后缀的图书编号的最小值。
    • n,q<1e3, 编号需求<1e7

    solution

    看完题第一个想法字典树模板,,然而不会写?
    再看,普及组的题?,,,范围1e3,,平方都能过吧,,怕不是个枚举。。。
    于是
    ——

    • 把图书编号从小到大排序
    • 对于每个需求码,从小到大遍历图书编号,判断是否为后缀(取出最后相应位数进行比较),若是则输出。(括号里坑点,不容易想到,,,%10^n取最后n位这个小常识。
      复杂度O(qn)

    codes

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int pow[] = {1,10,100,1000,10000,100000,1000000,10000000};//7位
    const int maxn = 1010;
    int a[maxn];
    int main(){
        int n, q;
        cin>>n>>q;
        for(int i = 1; i <= n; i++)cin>>a[i];
        sort(a+1,a+n+1);
        while(q--){
            int x, y, i;
            cin>>x>>y;
            for(i = 1; i <= n; i++)
                if(a[i]%pow[x] == y){cout<<a[i]<<'
    ';break;}
            if(i == n+1)cout<<"-1"<<'
    ';
        }
        return 0;
    }
  • 相关阅读:
    搜索专题
    KMP专题
    CSU 1326: The contest(分组背包)
    强连通专题
    MST:Bad Cowtractors(POJ 2377)
    MST:Agri-Net(POJ 1258)
    ShortestPath:Layout(POJ 3169)(差分约束的应用)
    MST:Conscription(POJ 3723)
    MST:Roadblocks(POJ 3255)
    DP:Space Elevator(POJ 2392)
  • 原文地址:https://www.cnblogs.com/gwj1314/p/9444630.html
Copyright © 2011-2022 走看看