【Codeforces 466C】Number of Ways
阿新 • • 發佈:2019-02-10
滿足 def int ber i+1 edr lose close tokenize
【鏈接】 我是鏈接,點我呀:)
【題意】
讓你把數組分成3個連續的部分
每個部分的和要一樣
問你有多少種分法
【題解】
先處理出來num[i]
表示i..n這裏面有多少個j
滿足aft[j] = aft[i]/2
這aft[i]=a[j]+a[j+1]..+a[n]
然後for從1..n
看看pre[i]*2=aft[i+1]是否成立。
如果成立的話那麽答案就加上num[i+1]
【代碼】
import java.io.*; import java.util.*; public class Main { static int N = (int)5e5; static InputReader in; static PrintWriter out; public static void main(String[] args) throws IOException{ in = new InputReader(); out = new PrintWriter(System.out); //code start from here new Task().solve(in, out); out.close(); } static class Task{ public void solve(InputReader in,PrintWriter out) { int n = in.nextInt(); int []a = new int[N+10]; long []pre = new long[N+10]; long []aft = new long[N+10]; int []num = new int[N+10]; for (int i = 1;i <= n;i++) a[i] = in.nextInt(); for (int i = 1;i <= n;i++) pre[i] = pre[i-1]+a[i]; for (int i = n;i >= 1;i--) aft[i] = aft[i+1]+a[i]; Hashtable<Long, Integer> dic = new Hashtable<Long,Integer>(); for (int i = n;i >= 1;i--) { if (aft[i]%2==0) { long temp = aft[i]/2; if (dic.get(temp)!=null) num[i] = dic.get(temp); } Integer temp1 = dic.get(aft[i]); if (temp1==null) { dic.put( aft[i], 1); }else { dic.put(aft[i], temp1+1); } } long ans = 0; for (int i = 1;i <=n;i++) { long cur = pre[i]; cur = pre[n]-cur; if (cur!=pre[i]*2) { continue; } ans = ans + num[i+1]; } out.println(ans); } } static class InputReader{ public BufferedReader br; public StringTokenizer tokenizer; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; } public String next(){ while (tokenizer==null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(br.readLine()); }catch(IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
【Codeforces 466C】Number of Ways