[计蒜客] ACM-ICPC 2018 南京赛区网络预赛 | 部分题解 | 线段树 + 线性筛 + 最短路(上)

简介: E. AC Challenge题目描述输入输出样例输入样例输出提示题意:A. An Olympian Math ProblemG. Lpl and Energy-saving Lamps题目描述输入输出样例输入样例输出提示ac_code:

E. AC Challenge


题目描述


Dlsj is competing in a contest with n (0 < n <= 20) problems. And he knows the answer of all of these problems.


However, he can submit i-th problem if and only if he has submitted (and passed, of course) si problems, the pi,1-th, pi,2-th, …, pi,si-th problem before.(0<pi,j≤n,0<j≤si,0<i≤n) After the submit of a problem, he has to wait for one minute, or cooling down time to submit another problem. As soon as the cooling down phase ended, he will submit his solution (and get “Accepted” of course) for the next problem he selected to solve or he will say that the contest is too easy and leave the arena.


“I wonder if I can leave the contest arena when the problems are too easy for me.”

“No problem.”

—— CCF NOI Problem set


If he submits and passes the i-th problem on t-th minute(or the t-th problem he solve is problem i), he can get t×ai + bi points. (|ai |,|bi |≤109)


Your task is to calculate the maximum number of points he can get in the contest.


输入


The first line of input contains an integer, n, which is the number of problems.

Then follows n lines, the i-th line contains si + 3 integers, ai,bi,si,p1,p2,…,psi as described in the description above.


输出


Output one line with one integer, the maximum number of points he can get in the contest.


样例输入


case1

5
5 6 0
4 5 1 1
3 4 1 2
2 3 1 3
1 2 1 4


case2

1
-100 0 0


样例输出


case1

55


case2

0


提示


In the first sample.

On the first minute, Dlsj submitted the first problem, and get 1×5+6=11 points.

On the second minute, Dlsj submitted the second problem, and get 2×4+5=13 points.

On the third minute, Dlsj submitted the third problem, and get 3×3+4=13 points.

On the forth minute, Dlsj submitted the forth problem, and get 4×2+3=11 points.

On the fifth minute, Dlsj submitted the fifth problem, and get 5×1+2=7 points.

So he can get 11+13+13+11+7=55 points in total.


In the second sample, you should note that he doesn’t have to solve all the problems.


题意:


有n个作业,每个作业都有两个属性值a,b,以及完成这个任务之前必须要完成其他的任务prei 然后完成一个作业获得的分数为 a*time + b,其中 time 为完成该任务的时间,问做多可以获取多少分数


刚看到这个题的时候以为是前一天遇到的那个题的类似版本,然后就用拓扑排序写了一下,然后根据在拓扑排序过程中的度数变化情况来对图进行分层次,然后对于每一个层次进行相应的操作。本来对同一个层次的作业进行贪心,按照a升序,b升序。可是这样子是不对的,因为有个变量(完成该任务的时间),所以这里就没有办法做了,思路gg

这道题也是非常的坎坷:

d15c72b89c5543f5bfe0b9689bae60d7.png


正确方法:状压dp

int getOne(ll x) {
  int ret = 0;
  while(x) {
    ret ++;
    x &= (x - 1);
  }
  return ret;
}
ll a[30],b[30];
ll c[30];
int n;
ll dp[maxn << 1];
int main() {
//  cout << getOne(15) << endl;
//  cout << (1LL<<63) << endl;
  n = read;
  for(int i=0; i<=n-1; i++) {
    a[i] = read,b[i] = read;
    int t = read;
    while(t --) {
      ll x = read;
      -- x;
      c[i] |= (1LL << x);
    }
  }
  ll ans = 0LL;
  ll lim = 1LL << n;///total 2 ** n;
  dp[0] = 0LL;
  for(int i=1; i<=lim; i++) dp[i] = inf_1;
  for(ll i=1; i<=lim-1; i++) {
//    dp[i] = 1<<31;
    for(ll j=0; j<n; j++) {
      if(!(i & (1LL << j))) continue;
      ll pre = i ^ (1LL << j);
//      debug(pre);
      if(dp[pre] == inf_1) continue;///pre status is -inf
      if((pre | c[j]) == pre) {/// pre has done
        ll time = getOne(i);
        ll val = dp[pre] + a[j] * time + b[j];
        dp[i] = max(dp[i],val);
      }
    }
  }
  for (int i=1; i<=lim; i++) {
    ans = max(ans,dp[i]);
  }
  cout << ans << endl;
  return 0;
}


A. An Olympian Math Problem


规律题,输入x xx,输出x − 1 x-1x−1


G. Lpl and Energy-saving Lamps


题目描述


During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Dragon imprisoned Lpl in the Castle? Dragon smiled enigmatically and answered that it is a big secret. After a pause, Dragon added:

— We have a contract. A rental agreement. He always works all day long. He likes silence. Besides that, there are many more advantages of living here in the Castle. Say, it is easy to justify a missed call: a phone ring can’t reach the other side of the Castle from where the phone has been left. So, the imprisonment is just a tale. Actually, he thinks about everything. He is smart. For instance, he started replacing incandescent lamps with energy-saving lamps in the whole Castle…

Lpl chose a model of energy-saving lamps and started the replacement as described below. He numbered all rooms in the Castle and counted how many lamps in each room he needs to replace.


At the beginning of each month, Lpl buys m energy-saving lamps and replaces lamps in rooms according to his list. He starts from the first room in his list. If the lamps in this room are not replaced yet and Lpl has enough energy-saving lamps to replace all lamps, then he replaces all ones and takes the room out from the list. Otherwise, he ‘ll just skip it and check the next room in his list. This process repeats until he has no energy-saving lamps or he has checked all rooms in his list. If he still has some energy-saving lamps after he has checked all rooms in his list, he’ll save the rest of energy-saving lamps for the next month.

As soon as all the work is done, he ceases buying new lamps. They are very high quality and have a very long-life cycle.


Your task is for a given number of month and descriptions of rooms to compute in how many rooms the old lamps will be replaced with energy-saving ones and how many energy-saving lamps will remain by the end of each month.


输入


Each input will consist of a single test case.


The first line contains integers n and m (1 ≤ n ≤ 100000, 1 ≤ m ≤ 100) — the number of rooms in the Castle and the number of energy-saving lamps, which Lpl buys monthly.


The second line contains n integers k1, k2, …, kn (1 ≤ kj ≤ 10000, j = 1, 2, …, n) — the number of lamps in the rooms of the Castle. The number in position j is the number of lamps in j-th room. Room numbers are given in accordance with Lpl’s list.

The third line contains one integer q (1 ≤ q ≤ 100000) — the number of queries.

The fourth line contains q integers d1, d2, …, dq (1 ≤ dp ≤ 100000, p = 1, 2, …, q) — numbers of months, in which queries are formed.


Months are numbered starting with 1; at the beginning of the first month Lpl buys the first m energy-saving lamps.


输出


Print q lines.

Line p contains two integers — the number of rooms, in which all old lamps are replaced already, and the number of remaining energy-saving lamps by the end of dp month.


样例输入


5 4
3 10 5 2 7
10
5 1 4 8 7 2 3 6 4 7


样例输出


4 0
1 1
3 6
5 1
5 1
2 0
3 2
4 4
3 6
5 1


提示


In the first month, he bought 4 energy-saving lamps and he replaced the first room in his list and remove it. And then he had 1 energy-saving lamps and skipped all rooms next. So, the answer for the first month is 1,1------1 room’s lamps were replaced already, 1 energy-saving lamp remain.


队友的线段树


ac_code:


const int maxn=1e5+7;
const int inf=0x3f3f3f3f;
#define ls  i<<1
#define rs  i<<1|1
ll n,m,a[maxn],q,x,y,id;
struct node{    ll x,y; }c[maxn];
struct tree{    ll l,r,mi;  }t[maxn<<4];
void pushup(ll i){  t[i].mi=min(t[ls].mi,t[rs].mi); }
void build(ll i,ll l,ll r){
    t[i].l=l;   t[i].r=r;
    if(l==r){
        t[i].mi=a[l];
        return;
    }
    ll mid=l+r>>1;
    build(ls,l,mid);    build(rs,mid+1,r);
    pushup(i);
}
void update(ll i,ll x){
    if(t[i].r<x||t[i].l>x)    return;
    if(t[i].l==t[i].r&&t[i].l==x){
        t[i].mi=inf;
        return;
    }
    update(ls,x);   update(rs,x);
    pushup(i);
}
ll ask(ll i,ll v){
    if(t[i].l==t[i].r)  return t[i].l;
    if(t[ls].mi<=v)      return ask(ls,v);
    return ask(rs,v);
}
int main(){
    n=read();   m=read();
    for(int i=1;i<=n;i++)    a[i]=read();
    build(1,1,n);
    for(int i=1;i<=100000;i++){
        x+=m;
        if(t[1].mi==inf)    x-=m;
        c[i].y=c[i-1].y;    c[i].x=x;
        while(x>=t[1].mi){
            ll t=ask(1,x);
            x-=a[t];    c[i].x-=a[t];
            c[i].y++;
            update(1,t);
        }
    }
    q=read();
    while(q--){
        ll i=read();
        printf("%lld %lld\n",c[i].y,c[i].x);
    }
}



目录
相关文章
|
机器学习/深度学习 Go
[计蒜客] ACM-ICPC 2019 南京赛区网络预赛 | 部分题解 | 线段树 + 线性筛 + 最短路(下)
J. Nanjing Sum 题目描述 输入 输出 样例输入 样例输出 提示 L. Magical Girl Haze 题目描述 输入 输出 样例输入 样例输出 题意:
153 0
[计蒜客] ACM-ICPC 2019 南京赛区网络预赛 | 部分题解 | 线段树 + 线性筛 + 最短路(下)
|
6天前
|
SQL 安全 网络安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
【10月更文挑战第40天】在数字化时代,网络安全和信息安全已成为我们生活中不可或缺的一部分。本文将介绍网络安全漏洞、加密技术以及安全意识等方面的知识,帮助读者更好地了解网络安全的重要性,并提供一些实用的技巧和建议,以保护个人和组织的信息安全。
29 6
|
8天前
|
存储 SQL 安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
【10月更文挑战第39天】在数字化时代,网络安全和信息安全成为了我们生活中不可或缺的一部分。本文将介绍网络安全漏洞、加密技术和安全意识等方面的内容,帮助读者更好地了解网络安全的重要性,并提供一些实用的技巧和方法来保护自己的信息安全。
21 2
|
9天前
|
安全 网络安全 数据安全/隐私保护
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
【10月更文挑战第38天】本文将探讨网络安全与信息安全的重要性,包括网络安全漏洞、加密技术和安全意识等方面。我们将通过代码示例和实际操作来展示如何保护网络和信息安全。无论你是个人用户还是企业,都需要了解这些知识以保护自己的网络安全和信息安全。
|
2天前
|
安全 算法 网络协议
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
在数字时代,网络安全和信息安全已经成为了我们生活中不可或缺的一部分。本文将介绍网络安全漏洞、加密技术和安全意识等方面的内容,帮助读者更好地了解网络安全的重要性和应对措施。通过阅读本文,您将了解到网络安全的基本概念、常见的网络安全漏洞、加密技术的原理和应用以及如何提高个人和组织的网络安全意识。
|
3天前
|
存储 安全 算法
网络安全与信息安全:漏洞、加密与意识的三重防线
在数字时代的浪潮中,网络安全与信息安全成为维护数据完整性、确保个人隐私和企业资产安全的基石。本文将深入探讨网络漏洞的成因、加密技术的应用以及安全意识的培养,旨在通过技术与教育的结合,构建起一道坚固的防御体系。我们将从实际案例出发,分析常见的网络安全威胁,揭示如何通过加密算法保护数据安全,并强调提升个人和组织的安全意识在防范网络攻击中的重要性。
|
3天前
|
监控 安全 网络安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
在数字时代,网络安全和信息安全已成为我们日常生活中不可或缺的一部分。本文将深入探讨网络安全漏洞、加密技术以及安全意识等方面的内容,帮助读者更好地了解如何保护自己的网络安全和信息安全。我们将通过实例和案例分析,揭示网络攻击者如何利用这些漏洞进行攻击,并提供一些实用的防御策略和建议。无论你是普通用户还是IT专业人士,这篇文章都将为你提供有价值的信息和见解。
|
4天前
|
SQL 安全 网络安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
【10月更文挑战第43天】在数字化时代,网络安全和信息安全已成为我们日常生活中不可或缺的一部分。本文将探讨网络安全的重要性,介绍常见的网络安全漏洞,以及如何通过加密技术和提高安全意识来保护我们的信息。我们将通过实际案例和代码示例来深入理解这些概念。无论你是IT专业人士还是普通用户,这篇文章都将为你提供有价值的知识和技能。
|
8天前
|
存储 安全 网络安全
云计算与网络安全:探索云服务中的信息安全策略
【10月更文挑战第39天】随着云计算的飞速发展,越来越多的企业和个人将数据和服务迁移到云端。然而,随之而来的网络安全问题也日益突出。本文将从云计算的基本概念出发,深入探讨在云服务中如何实施有效的网络安全和信息安全措施。我们将分析云服务模型(IaaS, PaaS, SaaS)的安全特性,并讨论如何在这些平台上部署安全策略。文章还将涉及最新的网络安全技术和实践,旨在为读者提供一套全面的云计算安全解决方案。
|
6天前
|
存储 SQL 安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
【10月更文挑战第41天】在数字化时代,网络安全与信息安全已成为我们生活的重要组成部分。本文将探讨网络安全漏洞、加密技术以及安全意识等方面的内容,并提供一些实用的建议和技巧,帮助读者提高网络安全防护能力。
15 2

热门文章

最新文章

下一篇
无影云桌面