Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
O(n2) is ok, dp, 516ms#include <iostream> #include <stdio.h> using namespace std; int N, T; int said[1005]; int mnLiar[2][1005][1005]; int mn(int a, int b){ return (a>b) ? b : a; } void init(){ for(int i = 0; i < 2; i++){ for(int j = 0; j < N; j++){ for(int k = 0; k < N; k++){ mnLiar[i][j][k] = -1; } } } } int get(int tag, int start, int end){ if(mnLiar[tag][start][end] != -1) return mnLiar[tag][start][end]; if(start == end){ if(tag){ mnLiar[tag][start][end] = 1; return 1; } else { mnLiar[tag][start][end] = 0; return 0; } } if(tag){ int res = mn(get(0, (start+1)%N, end), get(1, (start+1)%N, end)) + 1; mnLiar[tag][start][end] = res; return res; } else if(said[start]){ int res = get(1, (start+1)%N, end); mnLiar[tag][start][end] = res; return res; } else{ int res = get(0, (start+1)%N, end); mnLiar[tag][start][end] = res; return res; } } int main() { int t; scanf("%d", &t); for(int i = 0; i < t; i++){ scanf("%d%d", &N, &T); for(int j = 0; j < N; j++){ scanf("%d", &said[j]); } if(T == 0){ printf("0 0\n"); continue; } init(); int liarNum = 0; int mnLiar = -1; int offset, start, end; for(int j = 0; j < N; j++){ int det[1005] = {0}; bool kysz = 1; int jdgs = 0; int fwgs = 1; det[j] = 1; offset = j; while(1){ if(said[offset]){ offset = (offset+1)%N; if(det[offset] == 1){ kysz = 0; goto done; } else if(det[offset] == 0){ det[offset] = -1; jdgs++; fwgs++; if(jdgs > T) { kysz = 0; goto done; } } break; } else{ offset = (offset+1)%N; if(det[offset] == -1){ kysz = 0; goto done; } else if(det[offset] == 0){ det[offset] = 1; fwgs++; } } } start = (offset+1)%N; offset = (j+N-1)%N; if(said[offset]){ if(det[offset] == 1){ kysz = 0; goto done; } else if(det[offset] == 0){ det[offset] = -1; jdgs++; fwgs++; if(jdgs > T){ kysz = 0; goto done; } } offset = (offset+N-1)%N; while(!said[offset]){ if(det[offset] == 1){ kysz = 0; goto done; } else if(det[offset] == 0){ det[offset] = -1; jdgs++; fwgs++; if(jdgs > T){ kysz = 0; goto done; } } offset = (offset+N-1)%N; } end = offset; } else{ end = offset; } if(fwgs >= N) goto done; if(jdgs + mn(get(0, start, end), get(1, start, end)) > T) kysz = 0; done: if(!kysz){ liarNum++; if(mnLiar == -1) mnLiar = j; } if(liarNum == T) break; } printf("%d %d\n", liarNum, mnLiar+1); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator