銆愬仛棰樸€慳rc080_f-Prime Flip鈥斺€旇浆鎹€佹暟璁哄強鍖归厤

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愬仛棰樸€慳rc080_f-Prime Flip鈥斺€旇浆鎹€佹暟璁哄強鍖归厤相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/can' title='can'>can   return   fir   display   str   exp   oid   璐ㄦ暟   瀹炵幇   

棰樻剰锛氭湁涓€涓棤闄愬簭鍒楋紝鍏朵腑鏈?span class="math inline">(n)涓綅缃笂鐨勬暟涓?span class="math inline">(1)锛屽叾浣欓兘鏄?span class="math inline">(0)銆備綘鍙互杩涜鑻ュ共娆℃搷浣滐紝姣忔閫夊彇搴忓垪涓婄殑涓€涓尯闂?span class="math inline">([l,r))锛屾弧瓒?span class="math inline">(r-l)涓哄璐ㄦ暟锛屽皢鍦ㄨ繖涓尯闂翠笂鐨勬暟閮藉紓鎴栦笂(1)銆傞棶鏈€灏戣繘琛屽灏戞鎿嶄綔锛屼娇寰楀簭鍒椾笂鎵€鏈夋暟閮藉彉涓?span class="math inline">(0)銆?/p>

(n leq 100)

杩欑被鑷繁鍐冲畾鎿嶄綔鏉ヨ鏁板垪鍙樹负鍏ㄩ浂鐨勯棶棰橈紝鍦╝tcoder涓婃槸鎸哄父瑙佺殑銆?/p>

涓€鑸浜庤繖绉嶉棶棰橈紝瑕佺敤宸垎鎴栫被浼肩殑鎿嶄綔锛屾潵璁╁尯闂翠慨鏀瑰彉涓哄崟鐐逛慨鏀广€備緥濡傦紝瀵逛簬鍖洪棿鍔犵殑鎿嶄綔锛屽氨鐢ㄥ樊鍒嗗皢鍏跺彉涓轰袱涓崟鐐瑰姞銆傝嚦浜庤繖涓棶棰橈紝鎴戜滑璁惧師搴忓垪涓烘暟鍒?span class="math inline">({a})锛屽垯鏋勫缓鏁板垪({b})婊¤冻(b_n=a_{n-1} ,{ m xor} ,a_n)銆傝繖鏍凤紝棰樼洰涓殑([l,r))鍖洪棿寮傛垨(1)灏卞彉鎴愪簡灏?span class="math inline">(b_l)鍜?span class="math inline">(b_r)閮藉紓鎴栦笂(1)銆?/p>

鎺ヤ笅鏉ワ紝鎴戜滑鑰冭檻鎿嶄綔鍙互琚垎涓哄嚑涓儴鍒嗭紝姣忎竴涓儴鍒嗗氨鏄妸涓や釜涓?span class="math inline">(1)鐨勫厓绱犲彉涓?span class="math inline">(0)锛屽叾浣欏厓绱犲潎涓嶆敼鍙樼姸鎬併€備簬鏄紝闂灏卞彉鎴愪簡瀵?span class="math inline">({b})涓负(1)鐨勫厓绱犺繘琛屽尮閰嶃€傦紙鍙互鍙戠幇锛屼负(1)鐨勫厓绱犵殑鎬绘暟涓哄伓鏁帮級

鎴戜滑鑰冭檻涓や釜浣嶇疆鐨勮窛绂讳负(d)鐨勫厓绱狅紝灏嗕粬浠兘鍙樹负(0)锛岄渶瑕佸灏戞鎿嶄綔銆傝繖绛変环浜庢妸(d)琛ㄧず涓烘渶灏戞暟閲忕殑璐ㄦ暟鐨勫拰鎴栧樊銆?/p>

  • (d)涓哄璐ㄦ暟銆傞偅涔堬紝鏄剧劧涓€娆℃搷浣滃氨鍙互浜嗐€?/li>
  • (d)涓哄伓鏁般€傞鍏堬紝浠庡鍋舵€т笂鐪嬶紝鍙敤涓€娆℃搷浣滄槸涓嶅彲鑳界殑銆傛晠鑷冲皯闇€瑕佷袱娆℃搷浣溿€傝€冭檻(d=2)鏃讹紝鍙互鐢?span class="math inline">(2=5-3)涓ゆ鎿嶄綔瀹屾垚锛堝瓨鍦ㄤ綅缃鍏朵腑涓€涓厓绱犱负(5)锛屽彟涓€涓负(3)锛夈€傝€岃嫢(d geq 4)锛岀敱浜庡摜寰峰反璧寽鎯冲湪鏈鏁版嵁鑼冨洿鍐呮槸鎴愮珛鐨勶紝鏁呬竴瀹氳兘琛ㄧず涓轰袱涓川鏁扮殑鍜岋紝鍗冲彲浠ョ敤涓ゆ鎿嶄綔瀹炵幇銆?/li>
  • (d)涓哄鍚堟暟銆傛樉鐒朵竴娆℃搷浣滄垨涓ゆ鎿嶄綔閮芥槸涓嶅彲鑳界殑銆傝€?span class="math inline">(d)鍔犱笂涓€涓璐ㄦ暟鍚庢槸涓€涓伓鏁帮紝鏁呬篃鏈€澶氶渶瑕佷笁娆℃搷浣溿€?/li>

鍓╀笅鐨勫氨鏄尮閰嶇殑闂浜嗐€傝繖閲屼笉鑳芥媶鐐瑰仛浜屽垎鍥惧甫鏉冩渶澶у尮閰嶏紝寰堝鏄撹兘涓惧嚭鍙嶄緥銆備絾鏄紝鎴戜滑鍙互鐢ㄨ椽蹇冩潵鎶婃墍鏈夊厓绱犲垎涓轰袱缁勩€傦紙姝ゅ鐬勪簡棰樿В锛夎(k)涓烘墍鏈夊尮閰嶄腑(d)涓哄璐ㄦ暟鐨勭粍鏁般€傞偅涔堬紝鏄剧劧鏄?span class="math inline">(k)缁勫鏁颁綅缃拰鍋舵暟浣嶇疆鍖归厤銆傝濂囨暟鐨勪綅缃笂鏈?span class="math inline">(n_1)涓厓绱狅紝鍋舵暟鐨勪綅缃笂鏈?span class="math inline">(n_2)涓厓绱犮€傞偅涔堬紝鏄剧劧瑕佽濂囧伓鎬х浉鍚岀殑鍏冪礌鍖归厤鍒颁竴璧凤紝鎿嶄綔鎬绘暟鏈€灏忋€備簬鏄紝鎴戜滑鍙互寰楀嚭锛屾€绘搷浣滄暟涓?span class="math display">[k+2 imes (lfloor frac {n_0-k} {2} floor+lfloor frac {n_1-k} {2} floor) + 3 imes ((n_0 - k) mod 2)]

鐢变簬鍏冪礌鎬绘暟涓哄伓鏁帮紝(n_0)鍜?span class="math inline">(n_1)鍚屽鍋讹紝鎴戜滑鍒嗗鍋惰璁轰竴涓嬪氨鑳藉緱鍒板浜庢墍鏈夊悎娉曠殑(k)鍜?span class="math inline">(k+1)锛?span class="math inline">(f(k+1) leq f(k))銆傚洜姝わ紝鎴戜滑鍙鏈€澶у寲(k)灏卞彲浠ヤ簡銆傛垜浠牴鎹鍋舵€у鍏冪礌鍒嗙粍锛岀劧鍚庡仛浜屽垎鍥炬渶澶у尮閰嶅氨鍙互浜嗐€?/p>

鏃堕棿澶嶆潅搴?span class="math inline">(O(n^2 + m))銆?span class="math inline">(m)涓烘潈鍊煎ぇ灏忋€?/p>

#include <bits/stdc++.h>
using namespace std;
const int N = 610, INF = 0x3f3f3f3f;
int p[N],n,cnt,st,en,x[N];
struct edge {
  int la,b,cap;
} con[N * N * 2];
int tot=1,fir[N];
void add(int from,int to,int capc) {
  con[++tot] = (edge) {fir[from],to,capc};
  fir[from] = tot;
  con[++tot] = (edge) {fir[to],from,0};
  fir[to] = tot;
}
int cur[N], dis[N];
int dfs(int pos,int imp) {
  if (pos == en || (!imp)) return imp;
  int expo = 0, tmp;
  for (int &i = cur[pos] ; i ; i = con[i].la) {
    if (dis[con[i].b] == dis[pos] + 1) {
      tmp = dfs(con[i].b,min(imp,con[i].cap));
      con[i].cap -= tmp;
      con[i^1].cap += tmp;
      expo += tmp;
      imp -= tmp;
      if (!imp) break;
    }
  }
  return expo;
}
bool bfs() {
  static queue<int> q;
  while (!q.empty()) q.pop();
  memset(dis,0,sizeof dis);
  for (int i = 1 ; i <= n ; ++ i)
    cur[i] = fir[i];
  dis[st] = 1;
  q.push(st);
  for (int pos ; !q.empty() ; q.pop()) {
    pos = q.front();
    for (int i = fir[pos] ; i ; i = con[i].la) {
      if (con[i].cap && (!dis[con[i].b])) {
    dis[con[i].b] = dis[pos] + 1;
    q.push(con[i].b);
      }
    }
  }
  if (!dis[en]) return 0;
  return 1;
}
const int MAX = 10000010;
int isp[MAX + 10], pri[MAX / 10], pcnt, num[2];
set<int> prime;
void prework() {
  for (int i = 2 ; i <= MAX ; ++ i) {
    if (!isp[i]) pri[++pcnt] = i;
    for (int j = 1 ; j <= pcnt && pri[j] * i <= MAX ; ++ j) {
      isp[pri[j] * i] = 1;
      if (i % pri[j] == 0) break;
    }
  }
  for (int i = 2 ; i <= pcnt ; ++ i)
    prime.insert(pri[i]);
}
int main() {
  prework();
  scanf("%d",&n);
  for (int i = 1 ; i <= n ; ++ i) {
    scanf("%d",&x[i]);
    if (x[i] == 1 || x[i] != x[i-1] + 1)
      p[++cnt] = x[i];
    if (i > 1 && x[i] != x[i-1] + 1)
      p[++cnt] = x[i-1] + 1;
  }
  p[++cnt] = x[n] + 1;
  n = cnt;
  st = ++n;
  en = ++n;
  for (int i = 1 ; i <= cnt ; ++ i) {
    ++ num[p[i]&1];
    if (p[i]&1) {
      add(st,i,1);
      for (int j = 1 ; j <= cnt ; ++ j) {
    int d = abs(p[i] - p[j]);
    if (prime.count(d))
      add(i,j,1);
      }
    } else add(i,en,1);
  }
  int ans = 0;
  while (bfs())
    ans += dfs(st,INF);
  printf("%d
",ans + 2 * ((num[0] - ans) / 2 + (num[1] - ans) / 2) + ((num[1] - ans)&1) * 3);
  return 0;
}


灏忕粨锛氬張鐪嬩簡涓€閬撴€濈淮棰樼殑棰樿В鈥︹€︽劅瑙夎〃绠楅珮濡欑殑鍚屾椂锛岃嚜宸辫繕娣辨湁涓嶈冻銆?/p>


以上是关于銆愬仛棰樸€慳rc080_f-Prime Flip鈥斺€旇浆鎹€佹暟璁哄強鍖归厤的主要内容,如果未能解决你的问题,请参考以下文章

[Arc080F]Prime Flip

銆愬師鍒涖€慗ava骞跺彂缂栫▼绯诲垪30 | ThreadLocal

易經大意 061_080

080 对象的绑定方法

算法笔记_080:蓝桥杯练习 队列操作(Java)