contesthunter暑假NOIP模拟赛#1题解:

第一题:杯具大派送

水题。枚举A,B的公约数即可。

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
#define MAXN 100010
struct node
{
 int a,b,c;
}ans[MAXN];
int main() {
   int R, G;
   scanf("%d%d",&R,&G);
   int x=min(R,G);
   ,t=MAXN-;
   ; i*i<=x; ++i )
    {
       )
        {
          &&R%i==)
           {ans[s].a=i;
            ans[s].b=R/i;
            ans[s].c=G/i;
            s++;
           }
           &&R%(x/i)==)
           {
            ans[t].a=x/i;
            ans[t].b=R/(x/i);
            ans[t].c=G/(x/i);
            t--;
           }
         }
      }
   ;i>t;i--)
    printf("%d %d %d\n",ans[i].a,ans[i].b,ans[i].c);
        printf("%d %d %d\n",ans[i].a,ans[i].b,ans[i].c);
   ;
}

第二题:另类区间和

动态规划、记忆化搜索

官方题解:

Let f(n, prefix) consider all numbers in the interval [prefix·10n, (prefix+1)·10n>. The function calculates

the total contribution towards the result of n digits yet to be placed to the right of the given prefix.

To calculate f, we add a group of any digit other than the last one in prefix. For example, suppose n=4

and prefix=112. f(4, 112) considers the numbers 1120000 through 1129999. If we decide to append the

group 55 to the number, the contribution of this recursive branch is 52 + f(2, 11255).

It may seem that it takes too many recursive calls to calculate the result. However, many of these yield

the same result for different prefixes, more precisely when the entire interval considered is contained

inside [A, B]. For these cases we can memoize the result (it depends only on n). There are only

O(log B) recursive calls in which we branch without memoization.

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long llint;

llint A, B;
llint p10[16];
llint memo[16][11];

llint intersect( int n, llint prefix ) {
   if( n < 0 ) return 0;
   llint lo = max( prefix * p10[n], A );
   llint hi = min( (prefix+1) * p10[n] - 1, B );
   if( lo > hi ) return 0;
   return hi-lo+1;
}

llint rec( int n, int prev, llint prefix ) {
   llint mini = prefix * p10[n];
   llint maxi = (prefix+1) * p10[n] - 1;
   if( mini > B || maxi < A ) return 0;
   if( mini >= A && maxi <= B && memo[n][prev] != -1 ) return memo[n][prev];
   llint ret = 0;
   for( int digit = 0; digit <= 9; ++digit ) {
      if( digit == prev ) continue;
      llint t = prefix;
      for( int k = 1; k <= n; ++k ) {
         t = t*10+digit;
         ret += digit * k * k * (intersect( n-k, t )-intersect( n-k-1, t*10+digit)) + rec( n-k, digit, t );
      }
   }

   if( mini >= A && maxi <= B ) memo[n][prev] = ret;

   return ret;
}

int main( void ) {
   scanf( "%lld%lld", &A, &B );
   p10[0] = 1;
   for( int i = 1; i <= 15; ++i ) p10[i] = p10[i-1] * 10;
   memset( memo, -1, sizeof memo );
   printf( "%lld\n", rec( 15, 10, 0 ) );
   return 0;
}

第三题:01序列

线段树

交替出现的序列才是合法的序列,题目要求最长序列的长度。我们可以用线段树来解决。

当前区间最长序列长度应该等于下面三者的最大值:

左儿子区间的最长序列、右儿子区间的最长序列、以及

左儿子的后缀与右儿子的前缀组成的序列。

所以每个节点需要保存5个值:区间最长的合法序列的长度,前缀长度,后缀长度,前缀的第一个字符,后缀的最后一个字符。

其他的我们还需要知道前缀的最后一个字符,但我们可以通过前缀长度和前缀的第一个字符推导出来,后缀的第一个字符我们也可以通过后缀的最后字符和后缀长度推导出来。

标程:(01序列)

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int mxn = 1<<19;

struct node{
	int mx;
	int left, right;
	char start, end;
	int len;

} data[2*mxn];

node operator +( const node &a, const node &b ){
	node ret;
	ret.start = a.start;
	ret.end = b.end;
	ret.len = a.len + b.len;
	ret.left = a.left;
	if( a.len == a.left && a.end != b.start ) ret.left += b.left;

	ret.right = b.right;
	if( b.len == b.right && a.end != b.start ) ret.right += a.right;

	ret.mx = max( max( a.mx, b.mx ), max( ret.left, ret.right ) );
	if( a.end != b.start ) ret.mx = max( ret.mx, a.right+b.left );

	return ret;
}

int n;

void construct( int i, int lo, int hi ){
  if( hi-lo == 1 ){
    if( lo >= n ) data[i].mx = data[i].left = data[i].right = 0;
    else data[i].mx = data[i].left = data[i].right = 1;
    data[i].start = data[i].end = 'L';
    data[i].len = 1;
    return;
  }

  construct( 2*i, lo, (lo+hi)/2 );
  construct( 2*i+1, (lo+hi)/2, hi );
  data[i] = data[2*i] + data[2*i+1];
}

void change( int i ){
  i += mxn;
  if( data[i].start == 'L' ) data[i].start = 'R';
  else data[i].start = 'L';
  data[i].end = data[i].start;

  for( i /= 2; i > 0; i /= 2 )
    data[i] = data[2*i] + data[2*i+1];
}

int main(){
  int q;

  scanf( "%d %d", &n, &q );
  construct( 1, 0, mxn );

  for( ; q > 0; q-- ){
    int i;
    scanf( "%d", &i ); i--;

    change(i);
    printf( "%d\n", data[1].mx );
  }

  return 0;
}

contesthunter暑假NOIP模拟赛第一场题解的更多相关文章

  1. NOI.AC NOIP模拟赛 第一场 补记

    NOI.AC NOIP模拟赛 第一场 补记 candy 题目大意: 有两个超市,每个超市有\(n(n\le10^5)\)个糖,每个糖\(W\)元.每颗糖有一个愉悦度,其中,第一家商店中的第\(i\)颗 ...

  2. NOI.AC NOIP模拟赛 第二场 补记

    NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...

  3. nowcoder(牛客网)提高组模拟赛第一场 解题报告

    T1 中位数(二分) 这个题是一个二分(听说是上周atcoder beginner contest的D题???) 我们可以开一个数组b存a,sort然后二分b进行check(从后往前直接遍历check ...

  4. WC2019 全国模拟赛第一场 T1 题解

    由于只会T1,没法写游记,只好来写题解了... 题目链接 题目大意 给你一个数列,每次可以任取两个不相交的区间,取一次的贡献是这两个区间里所有数的最小值,求所有取法的贡献和,对 \(10^9+7\) ...

  5. nowcoder(牛客网)普及组模拟赛第一场 解题报告

    蒟蒻我可能考了一场假试 T1 绩点 这题没什么好说的,应该是只要会语言的就会做. T2 巨大的棋盘 一个模拟题吧qwq,但是要注意取模的时候先加上n或者m再取模,要不然会错的. #include< ...

  6. NOI.AC省选模拟赛第一场 T1 (树上高斯消元)

    link 很容易对于每个点列出式子 \(f_{x,y}=(f_{x,y-1}+f_{x,y}+f_{x,y+1}+f_{x+1,y})/4\)(边角转移类似,略) 这个转移是相互依赖的就gg了 不过你 ...

  7. CH Round #52 - Thinking Bear #1 (NOIP模拟赛)

    A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...

  8. CH Round #49 - Streaming #4 (NOIP模拟赛Day2)

    A.二叉树的的根 题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模 ...

  9. CH Round #48 - Streaming #3 (NOIP模拟赛Day1)

    A.数三角形 题目:http://www.contesthunter.org/contest/CH%20Round%20%2348%20-%20Streaming%20%233%20(NOIP模拟赛D ...

随机推荐

  1. Myeclipse里导入jQuery.js 时出现错误打红叉的解决方法

    http://www.jb51.net/softjc/215710.html 解决方案:如上图所示:右击 jquery-2.1.1.min.js -->MyEclipse --> ①先点击 ...

  2. hdu 5187 zhx's contest

    题目分析如果n=1,答案是1,否则答案是2n−2. 证明:ai肯定是最小的或者最大的.考虑另外的数,如果它们的位置定了的话,那么整个序列是唯一的. 那么ai是最小或者最大分别有2n−1种情况,而整个序 ...

  3. [转载] C++位运算:将一个4字节整数的二进制表示中的001替换为011

    #include <iostream> #include <assert.h> /** * Key: * * get someone bit: num & (mode1 ...

  4. Yii2中自带分页类实现分页

    1.首先写控制器层 先引用pagination类 use yii\data\Pagination; 写自己的方法: function actionFenye(){        $data = Fie ...

  5. JAVA常用运算符

    Java 语言中常用的运算符可分为如下几种: 1.算术运算符 int i = 5; int j = i++; // i = 6 j = 5 int j = ++i; // i = 6 j = 6 PS ...

  6. Git连接Github

    环境:Ubuntu Server 12.04 安装Git apt-get install git git-core 配置本机Git git config --global user.name &quo ...

  7. 附录二 C语言标准库

    上章回顾 数组和指针相同与不同 通过指针访问数组和通过数组访问指针 指针在什么时候可以加减运算 函数指针的申明和调用 函数数组和数组函数 git@github.com:Kevin-Dfg/Data-S ...

  8. Android开发之多级下拉列表菜单实现(仿美团,淘宝等)

    注:本文转载于:http://blog.csdn.net/minimicall/article/details/39484493 我们在常用的电商或者旅游APP中,例如美团,手机淘宝等等,都能够看的到 ...

  9. Python3字典中items()和python2.x中iteritems()有什么区别

    在Python2.x中,items( )用于 返回一个字典的拷贝列表[Returns a copy of the list of all items (key/value pairs) in D],占 ...

  10. .htaccess中301强制跳转到带www前缀或不带www的域名

    相信很多站长朋友都有这样的的问题出现.即带www前缀的域名与不带www前缀的顶级域名收录情况是不同的.这个问题主要是由于搜索引擎对于顶级域名与二级域名权重判定不同造成的.毫无疑问地,唯一的域名能够给你 ...